Skip to content

Commit a1e380c

Browse files
trflynn89ADKaster
authored andcommitted
Ladybird/WebDriver: Support running headless WebDriver sessions
This adds a dependency from WebDriver to Lagom's headless-browser to be used if the client's required capabilities indicate to do so.
1 parent 69cd0d6 commit a1e380c

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

Ladybird/WebContent/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QSocketNotifier>
3030
#include <QTimer>
3131
#include <WebContent/ConnectionFromClient.h>
32+
#include <WebContent/PageHost.h>
3233
#include <WebContent/WebDriverConnection.h>
3334

3435
static ErrorOr<void> load_content_filters();
@@ -103,7 +104,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
103104
QSocketNotifier webdriver_notifier(QSocketNotifier::Type::Read);
104105
RefPtr<WebContent::WebDriverConnection> webdriver_client;
105106
if (webdriver_fd_passing_socket >= 0)
106-
webdriver_client = TRY(create_connection_from_passed_socket<WebContent::WebDriverConnection>(webdriver_fd_passing_socket, "WebDriver"sv, webdriver_notifier, *webcontent_client, webcontent_client->page_host()));
107+
webdriver_client = TRY(create_connection_from_passed_socket<WebContent::WebDriverConnection>(webdriver_fd_passing_socket, "WebDriver"sv, webdriver_notifier, webcontent_client->page_host()));
107108

108109
return app.exec();
109110
}

Ladybird/WebDriver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(WEBDRIVER_SOURCE_DIR ${SERENITY_SOURCE_DIR}/Userland/Services/WebDriver)
33
set(SOURCES
44
${WEBDRIVER_SOURCE_DIR}/Client.cpp
55
${WEBDRIVER_SOURCE_DIR}/WebContentConnection.cpp
6+
../Utilities.cpp
67
Session.cpp
78
main.cpp
89
)
@@ -14,3 +15,4 @@ target_include_directories(WebDriver PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
1415
target_include_directories(WebDriver PRIVATE ${SERENITY_SOURCE_DIR}/Userland)
1516
target_include_directories(WebDriver PRIVATE ${SERENITY_SOURCE_DIR}/Userland/Services)
1617
target_link_libraries(WebDriver PRIVATE Qt::Core Qt::Network LibCore LibGfx LibIPC LibJS LibMain LibWeb LibWebSocket)
18+
add_dependencies(WebDriver headless-browser)

Ladybird/WebDriver/Session.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
#define AK_DONT_REPLACE_STD
1212

1313
#include "Session.h"
14+
#include "../Utilities.h"
1415
#include <LibCore/Stream.h>
1516
#include <LibCore/System.h>
1617
#include <WebDriver/Client.h>
1718
#include <unistd.h>
1819

1920
namespace WebDriver {
2021

21-
Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
22+
Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
2223
: m_client(move(client))
24+
, m_options(move(options))
2325
, m_id(session_id)
2426
{
2527
}
@@ -51,15 +53,39 @@ ErrorOr<void> Session::start()
5153

5254
auto fd_passing_socket_string = String::number(webcontent_fd_passing_fd);
5355

54-
char const* argv[] = {
55-
"ladybird",
56-
"--webdriver-fd-passing-socket",
57-
fd_passing_socket_string.characters(),
58-
nullptr,
59-
};
56+
if (m_options.headless) {
57+
auto resouces = String::formatted("{}/res", s_serenity_resource_root);
58+
auto error_page = String::formatted("{}/res/html/error.html", s_serenity_resource_root);
59+
auto certs = String::formatted("{}/etc/ca_certs.ini", s_serenity_resource_root);
60+
61+
char const* argv[] = {
62+
"headless-browser",
63+
"--resources",
64+
resouces.characters(),
65+
"--error-page",
66+
error_page.characters(),
67+
"--certs",
68+
certs.characters(),
69+
"--webdriver-fd-passing-socket",
70+
fd_passing_socket_string.characters(),
71+
"about:blank",
72+
nullptr,
73+
};
74+
75+
if (execvp("./_deps/lagom-build/headless-browser", const_cast<char**>(argv)) < 0)
76+
perror("execvp");
77+
} else {
78+
char const* argv[] = {
79+
"ladybird",
80+
"--webdriver-fd-passing-socket",
81+
fd_passing_socket_string.characters(),
82+
nullptr,
83+
};
84+
85+
if (execvp("./ladybird", const_cast<char**>(argv)) < 0)
86+
perror("execvp");
87+
}
6088

61-
if (execvp("./ladybird", const_cast<char**>(argv)) < 0)
62-
perror("execvp");
6389
VERIFY_NOT_REACHED();
6490
}
6591

Ladybird/WebDriver/Session.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace WebDriver {
1919

2020
class Session {
2121
public:
22-
Session(unsigned session_id, NonnullRefPtr<Client> client);
22+
Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options);
2323
~Session();
2424

2525
unsigned session_id() const { return m_id; }
@@ -35,8 +35,11 @@ class Session {
3535

3636
private:
3737
NonnullRefPtr<Client> m_client;
38+
Web::WebDriver::LadybirdOptions m_options;
39+
3840
bool m_started { false };
3941
unsigned m_id { 0 };
42+
4043
RefPtr<WebContentConnection> m_web_content_connection;
4144
Optional<pid_t> m_browser_pid;
4245
};

Ladybird/WebDriver/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#define AK_DONT_REPLACE_STD
88

9+
#include "../Utilities.h"
910
#include <LibCore/ArgsParser.h>
1011
#include <LibCore/EventLoop.h>
1112
#include <LibCore/System.h>
@@ -36,6 +37,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
3637
return 1;
3738
}
3839

40+
platform_init();
41+
3942
Core::EventLoop loop;
4043
auto server = TRY(Core::TCPServer::try_create());
4144

0 commit comments

Comments
 (0)