From b24a7079f1b889b2e55103c4729c9207f2b641fb Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 21 Oct 2024 19:07:55 -0400 Subject: [PATCH] LibWeb+WebDriver: Add a flag to default WebDriver to headless mode We previously only supported enabling headless mode on a per-session basis via the capabilities record. We don't have the ability to mutate this record from WPT, so this adds a flag to set the default mode. --- Ladybird/WebDriver/main.cpp | 5 +++++ Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp | 9 ++++++++- Userland/Libraries/LibWeb/WebDriver/Capabilities.h | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Ladybird/WebDriver/main.cpp b/Ladybird/WebDriver/main.cpp index 0bf0399486e..6a1d95c5c9f 100644 --- a/Ladybird/WebDriver/main.cpp +++ b/Ladybird/WebDriver/main.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include static Vector certificates; @@ -74,12 +75,14 @@ ErrorOr serenity_main(Main::Arguments arguments) auto listen_address = "0.0.0.0"sv; int port = 8000; bool force_cpu_painting = false; + bool headless = false; Core::ArgsParser args_parser; args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address"); args_parser.add_option(port, "Port to listen on", "port", 'p', "port"); args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.add_option(force_cpu_painting, "Launch browser with GPU painting disabled", "force-cpu-painting"); + args_parser.add_option(headless, "Launch browser without a graphical interface", "headless"); args_parser.parse(arguments); auto ipv4_address = IPv4Address::from_string(listen_address); @@ -95,6 +98,8 @@ ErrorOr serenity_main(Main::Arguments arguments) platform_init(); + Web::WebDriver::set_default_interface_mode(headless ? Web::WebDriver::InterfaceMode::Headless : Web::WebDriver::InterfaceMode::Graphical); + auto webdriver_socket_path = ByteString::formatted("{}/webdriver", TRY(Core::StandardPaths::runtime_directory())); TRY(Core::Directory::create(webdriver_socket_path, Core::Directory::CreateDirectories::Yes)); diff --git a/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp b/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp index dec68d01fd8..a812cd51dcf 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Capabilities.cpp @@ -72,6 +72,13 @@ static ErrorOr deserialize_as_a_proxy(JsonValue parameter) return proxy; } +static InterfaceMode default_interface_mode { InterfaceMode::Graphical }; + +void set_default_interface_mode(InterfaceMode interface_mode) +{ + default_interface_mode = interface_mode; +} + static Response deserialize_as_ladybird_options(JsonValue value) { if (!value.is_object()) @@ -88,7 +95,7 @@ static Response deserialize_as_ladybird_options(JsonValue value) static JsonObject default_ladybird_options() { JsonObject options; - options.set("headless"sv, false); + options.set("headless"sv, default_interface_mode == InterfaceMode::Headless); return options; } diff --git a/Userland/Libraries/LibWeb/WebDriver/Capabilities.h b/Userland/Libraries/LibWeb/WebDriver/Capabilities.h index 45cf316987e..27fb4a0a5e1 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(); } +enum class InterfaceMode { + Graphical, + Headless, +}; +void set_default_interface_mode(InterfaceMode); + struct LadybirdOptions { explicit LadybirdOptions(JsonObject const& capabilities);