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

WebContent+WebDriver: Move Execute Script to WebContent

This commit is contained in:
Timothy Flynn 2022-11-10 20:55:17 -05:00 committed by Linus Groh
parent 88dcdf681f
commit 0b9803dc93
6 changed files with 83 additions and 48 deletions

View file

@ -735,8 +735,7 @@ Web::WebDriver::Response Client::handle_execute_script(Vector<StringView> const&
{
dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/execute/sync");
auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->execute_script(payload));
return make_json_value(result);
return session->web_content_connection().execute_script(payload);
}
// 13.2.2 Execute Async Script, https://w3c.github.io/webdriver/#dfn-execute-async-script

View file

@ -329,47 +329,6 @@ static ErrorOr<ScriptArguments, Web::WebDriver::Error> extract_the_script_argume
return ScriptArguments { script, args };
}
// 13.2.1 Execute Script, https://w3c.github.io/webdriver/#dfn-execute-script
Web::WebDriver::Response Session::execute_script(JsonValue const& payload)
{
// 1. Let body and arguments be the result of trying to extract the script arguments from a request with argument parameters.
auto const& [body, arguments] = TRY(extract_the_script_arguments_from_a_request(payload));
// 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.3.
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 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, false);
dbgln("Executing 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:
case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result));
default:
VERIFY_NOT_REACHED();
}
}
// 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)
{

View file

@ -58,7 +58,6 @@ public:
Web::WebDriver::Response get_window_handle();
ErrorOr<void, Variant<Web::WebDriver::Error, Error>> close_window();
Web::WebDriver::Response get_window_handles() const;
Web::WebDriver::Response execute_script(JsonValue const& payload);
Web::WebDriver::Response execute_async_script(JsonValue const& payload);
Web::WebDriver::Response get_all_cookies();
Web::WebDriver::Response get_named_cookie(String const& name);