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