TimeoutsConfiguration.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 const& 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. if ((script_duration.is_number() && (script_duration.to_i64() < 0 || script_duration.to_i64() > max_safe_integer)) || !script_duration.is_null())
  43. return Error::from_code(ErrorCode::InvalidArgument, "Invalid script duration");
  44. // 3. Set timeouts’s script timeout to script duration.
  45. timeouts.script_timeout = script_duration.is_null() ? Optional<u64> {} : script_duration.to_u64();
  46. }
  47. // 4. If value has a property with the key "pageLoad":
  48. if (value.as_object().has("pageLoad"sv)) {
  49. // 1. Let page load duration be the value of property "pageLoad".
  50. auto const& page_load_duration = value.as_object().get("pageLoad"sv);
  51. // 2. If page load duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  52. if (!page_load_duration.is_number() || page_load_duration.to_i64() < 0 || page_load_duration.to_i64() > max_safe_integer)
  53. return Error::from_code(ErrorCode::InvalidArgument, "Invalid page load duration");
  54. // 3. Set timeouts’s page load timeout to page load duration.
  55. timeouts.page_load_timeout = page_load_duration.to_u64();
  56. }
  57. // 5. If value has a property with the key "implicit":
  58. if (value.as_object().has("implicit"sv)) {
  59. // 1. Let implicit duration be the value of property "implicit".
  60. auto const& implicit_duration = value.as_object().get("implicit"sv);
  61. // 2. If implicit duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  62. if (!implicit_duration.is_number() || implicit_duration.to_i64() < 0 || implicit_duration.to_i64() > max_safe_integer)
  63. return Error::from_code(ErrorCode::InvalidArgument, "Invalid implicit duration");
  64. // 3. Set timeouts’s implicit wait timeout to implicit duration.
  65. timeouts.implicit_wait_timeout = implicit_duration.to_u64();
  66. }
  67. // 6. Return success with data timeouts.
  68. return timeouts;
  69. }
  70. }