mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:47:44 +00:00
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.
This commit is contained in:
parent
e840d27d8e
commit
7edd57dc87
6 changed files with 76 additions and 15 deletions
|
@ -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<JsonObject, Error> validate_capabilities(JsonValue const& capability)
|
||||
{
|
||||
|
@ -118,8 +139,12 @@ static ErrorOr<JsonObject, Error> 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<void> {
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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> session = make<Session>(session_id, *this);
|
||||
auto start_result = session->start();
|
||||
if (start_result.is_error()) {
|
||||
Web::WebDriver::LadybirdOptions options { capabilities.as_object() };
|
||||
auto session = make<Session>(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();
|
||||
|
||||
|
|
|
@ -17,8 +17,9 @@
|
|||
|
||||
namespace WebDriver {
|
||||
|
||||
Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
|
||||
Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
|
||||
: m_client(move(client))
|
||||
, m_options(move(options))
|
||||
, m_id(session_id)
|
||||
{
|
||||
}
|
||||
|
@ -63,14 +64,26 @@ ErrorOr<void> 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,
|
||||
};
|
||||
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/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
|
||||
m_browser_pid = TRY(Core::System::posix_spawn("/bin/headless-browser"sv, nullptr, nullptr, const_cast<char**>(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<char**>(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.
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <AK/Error.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibCore/Promise.h>
|
||||
#include <LibWeb/WebDriver/Capabilities.h>
|
||||
#include <LibWeb/WebDriver/Error.h>
|
||||
#include <LibWeb/WebDriver/Response.h>
|
||||
#include <WebDriver/WebContentConnection.h>
|
||||
|
@ -20,7 +21,7 @@ namespace WebDriver {
|
|||
|
||||
class Session {
|
||||
public:
|
||||
Session(unsigned session_id, NonnullRefPtr<Client> client);
|
||||
Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options);
|
||||
~Session();
|
||||
|
||||
unsigned session_id() const { return m_id; }
|
||||
|
@ -39,8 +40,11 @@ private:
|
|||
ErrorOr<NonnullRefPtr<Core::LocalServer>> create_server(String const& socket_path, NonnullRefPtr<ServerPromise> promise);
|
||||
|
||||
NonnullRefPtr<Client> m_client;
|
||||
Web::WebDriver::LadybirdOptions m_options;
|
||||
|
||||
bool m_started { false };
|
||||
unsigned m_id { 0 };
|
||||
|
||||
RefPtr<WebContentConnection> m_web_content_connection;
|
||||
Optional<pid_t> m_browser_pid;
|
||||
};
|
||||
|
|
|
@ -71,6 +71,7 @@ ErrorOr<int> 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"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue