1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 10:57:36 +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

@ -18,7 +18,6 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/JSONObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Cookie/ParsedCookie.h>
#include <LibWeb/DOM/Document.h>
@ -574,24 +573,4 @@ void ConnectionFromClient::set_system_visibility_state(bool visible)
: Web::HTML::VisibilityState::Hidden);
}
Messages::WebContentServer::WebdriverExecuteScriptResponse ConnectionFromClient::webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async)
{
auto& page = m_page_host->page();
auto* window = page.top_level_browsing_context().active_window();
auto& vm = window->vm();
auto arguments = JS::MarkedVector<JS::Value> { vm.heap() };
for (auto const& argument_string : json_arguments) {
// NOTE: These are assumed to be valid JSON values.
auto json_value = MUST(JsonValue::from_string(argument_string));
arguments.append(JS::JSONObject::parse_json_value(vm, json_value));
}
auto result = async
? Web::WebDriver::execute_async_script(page, body, move(arguments), timeout)
: Web::WebDriver::execute_script(page, body, move(arguments), timeout);
return { result.type, result.value.serialized<StringBuilder>() };
}
}

View file

@ -90,8 +90,6 @@ private:
virtual Messages::WebContentServer::GetSelectedTextResponse get_selected_text() override;
virtual void select_all() override;
virtual Messages::WebContentServer::WebdriverExecuteScriptResponse webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async) override;
void flush_pending_paint_requests();
NonnullOwnPtr<PageHost> m_page_host;

View file

@ -43,8 +43,6 @@ endpoint WebContentServer
take_document_screenshot() => (Gfx::ShareableBitmap data)
webdriver_execute_script(String body, Vector<String> json_arguments, Optional<u64> timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)
run_javascript(String js_source) =|
dump_layout_tree() => (String dump)

View file

@ -23,6 +23,7 @@ endpoint WebDriverClient {
is_element_enabled(String element_id) => (Web::WebDriver::Response response)
get_source() => (Web::WebDriver::Response response)
execute_script(JsonValue payload) => (Web::WebDriver::Response response)
execute_async_script(JsonValue payload) => (Web::WebDriver::Response response)
take_screenshot() => (Web::WebDriver::Response response)
take_element_screenshot(String element_id) => (Web::WebDriver::Response response)
}

View file

@ -797,6 +797,38 @@ Messages::WebDriverClient::ExecuteScriptResponse WebDriverConnection::execute_sc
VERIFY_NOT_REACHED();
}
// 13.2.2 Execute Async Script, https://w3c.github.io/webdriver/#dfn-execute-async-script
Messages::WebDriverClient::ExecuteAsyncScriptResponse WebDriverConnection::execute_async_script(JsonValue const& payload)
{
// 1. Let body and arguments by 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(ensure_open_top_level_browsing_context());
// FIXME: 3. Handle any user prompts, and return its value if it is an error.
// 4., 5.1-5.11.
// FIXME: Move timeouts from WebDriver to WebContent and pass the script timeout through here.
auto result = Web::WebDriver::execute_async_script(m_page_host.page(), body, move(arguments), {});
dbgln_if(WEBDRIVER_DEBUG, "Executing async script returned: {}", result.value);
switch (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 make_success_response(move(result.value));
// 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.value));
}
VERIFY_NOT_REACHED();
}
// 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_screenshot()
{

View file

@ -55,6 +55,7 @@ private:
virtual Messages::WebDriverClient::IsElementEnabledResponse is_element_enabled(String const& element_id) override;
virtual Messages::WebDriverClient::GetSourceResponse get_source() override;
virtual Messages::WebDriverClient::ExecuteScriptResponse execute_script(JsonValue const& payload) override;
virtual Messages::WebDriverClient::ExecuteAsyncScriptResponse execute_async_script(JsonValue const& payload) override;
virtual Messages::WebDriverClient::TakeScreenshotResponse take_screenshot() override;
virtual Messages::WebDriverClient::TakeElementScreenshotResponse take_element_screenshot(String const& element_id) override;