From 7edd57dc87f738b2a3e28aa533da2f3a529eaf41 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 22 Nov 2022 07:38:07 -0500 Subject: LibWeb+WebDriver: Support running headless WebDriver sessions This adds an "extension capability" for clients to indicate that a headless browser should be used for the session. --- .../Libraries/LibWeb/WebDriver/Capabilities.cpp | 41 ++++++++++++++++++++-- Userland/Libraries/LibWeb/WebDriver/Capabilities.h | 6 ++++ Userland/Services/WebDriver/Client.cpp | 8 ++--- Userland/Services/WebDriver/Session.cpp | 31 +++++++++++----- Userland/Services/WebDriver/Session.h | 6 +++- Userland/Services/WebDriver/main.cpp | 1 + 6 files changed, 77 insertions(+), 16 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp b/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp index f641157ada..6e22f68765 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp @@ -45,6 +45,27 @@ static Response deserialize_as_an_unhandled_prompt_behavior(JsonValue value) return value; } +static Response deserialize_as_ladybird_options(JsonValue value) +{ + if (!value.is_object()) + return Error::from_code(ErrorCode::InvalidArgument, "Extension capability serenity:ladybird must be an object"sv); + + auto const& object = value.as_object(); + + if (auto const* headless = object.get_ptr("headless"sv); headless && !headless->is_bool()) + return Error::from_code(ErrorCode::InvalidArgument, "Extension capability serenity:ladybird/headless must be a boolean"sv); + + return value; +} + +static JsonObject default_ladybird_options() +{ + JsonObject options; + options.set("headless"sv, false); + + return options; +} + // https://w3c.github.io/webdriver/#dfn-validate-capabilities static ErrorOr validate_capabilities(JsonValue const& capability) { @@ -118,8 +139,12 @@ static ErrorOr validate_capabilities(JsonValue const& capabil // FIXME: -> name is the name of an additional WebDriver capability // FIXME: Let deserialized be the result of trying to run the additional capability deserialization algorithm for the extension capability corresponding to name, with argument value. - // FIXME: -> name is the key of an extension capability - // FIXME: If name is known to the implementation, let deserialized be the result of trying to deserialize value in an implementation-specific way. Otherwise, let deserialized be set to value. + + // -> name is the key of an extension capability + // If name is known to the implementation, let deserialized be the result of trying to deserialize value in an implementation-specific way. Otherwise, let deserialized be set to value. + else if (name == "serenity:ladybird"sv) { + deserialized = TRY(deserialize_as_ladybird_options(value)); + } // -> The remote end is an endpoint node else { @@ -232,6 +257,7 @@ static JsonValue match_capabilities(JsonObject const& capabilities) matched_capabilities.set("setWindowRect"sv, true); // 2. Optionally add extension capabilities as entries to matched capabilities. The values of these may be elided, and there is no requirement that all extension capabilities be added. + matched_capabilities.set("serenity:ladybird"sv, default_ladybird_options()); // 3. For each name and value corresponding to capability’s own properties: auto result = capabilities.try_for_each_member([&](auto const& name, auto const& value) -> ErrorOr { @@ -366,4 +392,15 @@ Response process_capabilities(JsonValue const& parameters) return JsonValue {}; } +LadybirdOptions::LadybirdOptions(JsonObject const& capabilities) +{ + auto const* options = capabilities.get_ptr("serenity:ladybird"sv); + if (!options || !options->is_object()) + return; + + auto const* headless = options->as_object().get_ptr("headless"sv); + if (headless && headless->is_bool()) + this->headless = headless->as_bool(); +} + } diff --git a/Userland/Libraries/LibWeb/WebDriver/Capabilities.h b/Userland/Libraries/LibWeb/WebDriver/Capabilities.h index 72153f688b..45cf316987 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Capabilities.h +++ b/Userland/Libraries/LibWeb/WebDriver/Capabilities.h @@ -54,6 +54,12 @@ constexpr UnhandledPromptBehavior unhandled_prompt_behavior_from_string(StringVi VERIFY_NOT_REACHED(); } +struct LadybirdOptions { + explicit LadybirdOptions(JsonObject const& capabilities); + + bool headless { false }; +}; + Response process_capabilities(JsonValue const& parameters); } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 6ee64e8073..66fe5a33a7 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -151,11 +151,11 @@ Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonVal auto session_id = Client::s_next_session_id++; // 7. Let session be a new session with the session ID of session id. - NonnullOwnPtr session = make(session_id, *this); - auto start_result = session->start(); - if (start_result.is_error()) { + Web::WebDriver::LadybirdOptions options { capabilities.as_object() }; + auto session = make(session_id, *this, move(options)); + + if (auto start_result = session->start(); start_result.is_error()) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::SessionNotCreated, String::formatted("Failed to start session: {}", start_result.error().string_literal())); - } auto& web_content_connection = session->web_content_connection(); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index a96bb49819..1636b998b0 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -17,8 +17,9 @@ namespace WebDriver { -Session::Session(unsigned session_id, NonnullRefPtr client) +Session::Session(unsigned session_id, NonnullRefPtr client, Web::WebDriver::LadybirdOptions options) : m_client(move(client)) + , m_options(move(options)) , m_id(session_id) { } @@ -63,14 +64,26 @@ ErrorOr Session::start() auto web_content_socket_path = String::formatted("/tmp/webdriver/session_{}_{}", getpid(), m_id); auto web_content_server = TRY(create_server(web_content_socket_path, promise)); - char const* argv[] = { - "/bin/Browser", - "--webdriver-content-path", - web_content_socket_path.characters(), - nullptr, - }; - - m_browser_pid = TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast(argv), environ)); + if (m_options.headless) { + char const* argv[] = { + "/bin/headless-browser", + "--webdriver-ipc-path", + web_content_socket_path.characters(), + "about:blank", + nullptr, + }; + + m_browser_pid = TRY(Core::System::posix_spawn("/bin/headless-browser"sv, nullptr, nullptr, const_cast(argv), environ)); + } else { + char const* argv[] = { + "/bin/Browser", + "--webdriver-content-path", + web_content_socket_path.characters(), + nullptr, + }; + + m_browser_pid = TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast(argv), environ)); + } // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate // errors received while accepting the Browser and WebContent sockets. diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index f039edbdf6..dc5b1f849d 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +21,7 @@ namespace WebDriver { class Session { public: - Session(unsigned session_id, NonnullRefPtr client); + Session(unsigned session_id, NonnullRefPtr client, Web::WebDriver::LadybirdOptions options); ~Session(); unsigned session_id() const { return m_id; } @@ -39,8 +40,11 @@ private: ErrorOr> create_server(String const& socket_path, NonnullRefPtr promise); NonnullRefPtr m_client; + Web::WebDriver::LadybirdOptions m_options; + bool m_started { false }; unsigned m_id { 0 }; + RefPtr m_web_content_connection; Optional m_browser_pid; }; diff --git a/Userland/Services/WebDriver/main.cpp b/Userland/Services/WebDriver/main.cpp index 27ede006bd..8306c4b521 100644 --- a/Userland/Services/WebDriver/main.cpp +++ b/Userland/Services/WebDriver/main.cpp @@ -71,6 +71,7 @@ ErrorOr serenity_main(Main::Arguments arguments) outln("Listening on {}:{}", ipv4_address.value(), port); TRY(Core::System::unveil("/bin/Browser", "rx")); + TRY(Core::System::unveil("/bin/headless-browser", "rx")); TRY(Core::System::unveil("/etc/timezone", "r")); TRY(Core::System::unveil("/res/icons", "r")); TRY(Core::System::unveil("/tmp/webdriver", "rwc")); -- cgit v1.2.3