1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

Browser+WebContent+WebDriver: Move Execute Async Script to WebContent

With this, WebDriverEndpoints is unused and removed :^)
This commit is contained in:
Timothy Flynn 2022-11-10 21:00:27 -05:00 committed by Linus Groh
parent 0b9803dc93
commit 31469ee45a
17 changed files with 35 additions and 161 deletions

View file

@ -297,78 +297,6 @@ Web::WebDriver::Response Session::get_window_handles() const
return JsonValue { handles };
}
struct ScriptArguments {
String script;
JsonArray const& arguments;
};
// https://w3c.github.io/webdriver/#dfn-extract-the-script-arguments-from-a-request
static ErrorOr<ScriptArguments, Web::WebDriver::Error> extract_the_script_arguments_from_a_request(JsonValue const& payload)
{
if (!payload.is_object())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
auto const& properties = payload.as_object();
// 1. Let script be the result of getting a property named script from the parameters.
// 2. If script is not a String, return error with error code invalid argument.
if (!properties.has_string("script"sv))
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have a 'script' string property");
auto script = properties.get("script"sv).as_string();
// 3. Let args be the result of getting a property named args from the parameters.
// 4. If args is not an Array return error with error code invalid argument.
if (!properties.has_array("args"sv))
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have an 'args' string property");
auto const& args = properties.get("args"sv).as_array();
// 5. Let arguments be the result of calling the JSON deserialize algorithm with arguments args.
// NOTE: We forward the JSON array to the Browser and then WebContent process over IPC, so this is not necessary.
// 6. Return success with data script and arguments.
return ScriptArguments { script, args };
}
// 13.2.2 Execute Async Script, https://w3c.github.io/webdriver/#dfn-execute-async-script
Web::WebDriver::Response Session::execute_async_script(JsonValue const& parameters)
{
// 1. Let body and arguments by the result of trying to extract the script arguments from a request with argument parameters.
auto [body, arguments] = TRY(extract_the_script_arguments_from_a_request(parameters));
// 2. If the current browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 3. Handle any user prompts, and return its value if it is an error.
// 4., 5.1-5.11.
Vector<String> json_arguments;
arguments.for_each([&](JsonValue const& json_value) {
// NOTE: serialized() instead of to_string() ensures proper quoting.
json_arguments.append(json_value.serialized<StringBuilder>());
});
dbgln("Executing async script with 'args': [{}] / 'body':\n{}", String::join(", "sv, json_arguments), body);
auto execute_script_response = m_browser_connection->execute_script(body, json_arguments, m_timeouts_configuration.script_timeout, true);
dbgln("Executing async script returned: {}", execute_script_response.json_result());
// NOTE: This is assumed to be a valid JSON value.
auto result = MUST(JsonValue::from_string(execute_script_response.json_result()));
switch (execute_script_response.result_type()) {
// 6. If promise is still pending and the session script timeout is reached, return error with error code script timeout.
case Web::WebDriver::ExecuteScriptResultType::Timeout:
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out");
// 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseResolved:
return result;
// 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result));
default:
VERIFY_NOT_REACHED();
}
}
// https://w3c.github.io/webdriver/#dfn-serialized-cookie
static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
{