1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:27:35 +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:
Timothy Flynn 2022-11-22 07:38:07 -05:00 committed by Linus Groh
parent e840d27d8e
commit 7edd57dc87
6 changed files with 76 additions and 15 deletions

View file

@ -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();

View file

@ -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.

View file

@ -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;
};

View file

@ -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"));