TimeoutsConfiguration.cpp 4.2 KB

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