TimeoutsConfiguration.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <LibWeb/WebDriver/TimeoutsConfiguration.h>
  8. namespace Web::WebDriver {
  9. // https://w3c.github.io/webdriver/#dfn-timeouts-object
  10. JsonObject timeouts_object(TimeoutsConfiguration const& timeouts)
  11. {
  12. // The timeouts object for a timeouts configuration timeouts is an object initialized with the following properties:
  13. auto timeouts_object = JsonObject {};
  14. // "script"
  15. // timeouts' script timeout value, if set, or its default value.
  16. if (timeouts.script_timeout.has_value())
  17. timeouts_object.set("script", *timeouts.script_timeout);
  18. else
  19. timeouts_object.set("script", JsonValue {});
  20. // "pageLoad"
  21. // timeouts' page load timeout’s value, if set, or its default value.
  22. timeouts_object.set("pageLoad", timeouts.page_load_timeout);
  23. // "implicit"
  24. // timeouts' implicit wait timeout’s value, if set, or its default value.
  25. timeouts_object.set("implicit", timeouts.implicit_wait_timeout);
  26. return timeouts_object;
  27. }
  28. // https://w3c.github.io/webdriver/#ref-for-dfn-json-deserialize-3
  29. ErrorOr<TimeoutsConfiguration, Error> json_deserialize_as_a_timeouts_configuration(JsonValue const& value)
  30. {
  31. constexpr i64 max_safe_integer = 9007199254740991;
  32. // 1. Let timeouts be a new timeouts configuration.
  33. auto timeouts = TimeoutsConfiguration {};
  34. // 2. If value is not a JSON Object, return error with error code invalid argument.
  35. if (!value.is_object())
  36. return Error::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object");
  37. // 3. If value has a property with the key "script":
  38. if (value.as_object().has("script"sv)) {
  39. // 1. Let script duration be the value of property "script".
  40. auto script_duration = value.as_object().get("script"sv);
  41. // 2. If script duration is a number and less than 0 or greater than maximum safe integer, or it is not null, return error with error code invalid argument.
  42. Optional<u64> script_timeout;
  43. if (script_duration.has_value()) {
  44. bool is_valid;
  45. if (auto duration = script_duration->get_double_with_precision_loss(); duration.has_value()) {
  46. is_valid = *duration >= 0 && *duration <= max_safe_integer;
  47. // FIXME: script_timeout should be double.
  48. script_timeout = static_cast<u64>(*duration);
  49. } else if (script_duration->is_null()) {
  50. is_valid = true;
  51. } else {
  52. is_valid = false;
  53. }
  54. if (!is_valid)
  55. return Error::from_code(ErrorCode::InvalidArgument, "Invalid script duration");
  56. }
  57. // 3. Set timeouts’s script timeout to script duration.
  58. timeouts.script_timeout = script_timeout;
  59. }
  60. // 4. If value has a property with the key "pageLoad":
  61. if (value.as_object().has("pageLoad"sv)) {
  62. // 1. Let page load duration be the value of property "pageLoad".
  63. auto page_load_duration = value.as_object().get_i64("pageLoad"sv);
  64. // 2. If page load duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  65. if (!page_load_duration.has_value() || *page_load_duration < 0 || *page_load_duration > max_safe_integer)
  66. return Error::from_code(ErrorCode::InvalidArgument, "Invalid page load duration");
  67. // 3. Set timeouts’s page load timeout to page load duration.
  68. timeouts.page_load_timeout = static_cast<u64>(*page_load_duration);
  69. }
  70. // 5. If value has a property with the key "implicit":
  71. if (value.as_object().has("implicit"sv)) {
  72. // 1. Let implicit duration be the value of property "implicit".
  73. auto implicit_duration = value.as_object().get_i64("implicit"sv);
  74. // 2. If implicit duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  75. if (!implicit_duration.has_value() || *implicit_duration < 0 || *implicit_duration > max_safe_integer)
  76. return Error::from_code(ErrorCode::InvalidArgument, "Invalid implicit duration");
  77. // 3. Set timeouts’s implicit wait timeout to implicit duration.
  78. timeouts.implicit_wait_timeout = static_cast<u64>(*implicit_duration);
  79. }
  80. // 6. Return success with data timeouts.
  81. return timeouts;
  82. }
  83. }