瀏覽代碼

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.
Timothy Flynn 2 年之前
父節點
當前提交
a1e380cc38

+ 2 - 1
Ladybird/WebContent/main.cpp

@@ -29,6 +29,7 @@
 #include <QSocketNotifier>
 #include <QTimer>
 #include <WebContent/ConnectionFromClient.h>
+#include <WebContent/PageHost.h>
 #include <WebContent/WebDriverConnection.h>
 
 static ErrorOr<void> load_content_filters();
@@ -103,7 +104,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     QSocketNotifier webdriver_notifier(QSocketNotifier::Type::Read);
     RefPtr<WebContent::WebDriverConnection> webdriver_client;
     if (webdriver_fd_passing_socket >= 0)
-        webdriver_client = TRY(create_connection_from_passed_socket<WebContent::WebDriverConnection>(webdriver_fd_passing_socket, "WebDriver"sv, webdriver_notifier, *webcontent_client, webcontent_client->page_host()));
+        webdriver_client = TRY(create_connection_from_passed_socket<WebContent::WebDriverConnection>(webdriver_fd_passing_socket, "WebDriver"sv, webdriver_notifier, webcontent_client->page_host()));
 
     return app.exec();
 }

+ 2 - 0
Ladybird/WebDriver/CMakeLists.txt

@@ -3,6 +3,7 @@ set(WEBDRIVER_SOURCE_DIR ${SERENITY_SOURCE_DIR}/Userland/Services/WebDriver)
 set(SOURCES
     ${WEBDRIVER_SOURCE_DIR}/Client.cpp
     ${WEBDRIVER_SOURCE_DIR}/WebContentConnection.cpp
+    ../Utilities.cpp
     Session.cpp
     main.cpp
 )
@@ -14,3 +15,4 @@ target_include_directories(WebDriver PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
 target_include_directories(WebDriver PRIVATE ${SERENITY_SOURCE_DIR}/Userland)
 target_include_directories(WebDriver PRIVATE ${SERENITY_SOURCE_DIR}/Userland/Services)
 target_link_libraries(WebDriver PRIVATE Qt::Core Qt::Network LibCore LibGfx LibIPC LibJS LibMain LibWeb LibWebSocket)
+add_dependencies(WebDriver headless-browser)

+ 35 - 9
Ladybird/WebDriver/Session.cpp

@@ -11,6 +11,7 @@
 #define AK_DONT_REPLACE_STD
 
 #include "Session.h"
+#include "../Utilities.h"
 #include <LibCore/Stream.h>
 #include <LibCore/System.h>
 #include <WebDriver/Client.h>
@@ -18,8 +19,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)
 {
 }
@@ -51,15 +53,39 @@ ErrorOr<void> Session::start()
 
         auto fd_passing_socket_string = String::number(webcontent_fd_passing_fd);
 
-        char const* argv[] = {
-            "ladybird",
-            "--webdriver-fd-passing-socket",
-            fd_passing_socket_string.characters(),
-            nullptr,
-        };
+        if (m_options.headless) {
+            auto resouces = String::formatted("{}/res", s_serenity_resource_root);
+            auto error_page = String::formatted("{}/res/html/error.html", s_serenity_resource_root);
+            auto certs = String::formatted("{}/etc/ca_certs.ini", s_serenity_resource_root);
+
+            char const* argv[] = {
+                "headless-browser",
+                "--resources",
+                resouces.characters(),
+                "--error-page",
+                error_page.characters(),
+                "--certs",
+                certs.characters(),
+                "--webdriver-fd-passing-socket",
+                fd_passing_socket_string.characters(),
+                "about:blank",
+                nullptr,
+            };
+
+            if (execvp("./_deps/lagom-build/headless-browser", const_cast<char**>(argv)) < 0)
+                perror("execvp");
+        } else {
+            char const* argv[] = {
+                "ladybird",
+                "--webdriver-fd-passing-socket",
+                fd_passing_socket_string.characters(),
+                nullptr,
+            };
+
+            if (execvp("./ladybird", const_cast<char**>(argv)) < 0)
+                perror("execvp");
+        }
 
-        if (execvp("./ladybird", const_cast<char**>(argv)) < 0)
-            perror("execvp");
         VERIFY_NOT_REACHED();
     }
 

+ 4 - 1
Ladybird/WebDriver/Session.h

@@ -19,7 +19,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; }
@@ -35,8 +35,11 @@ public:
 
 private:
     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;
 };

+ 3 - 0
Ladybird/WebDriver/main.cpp

@@ -6,6 +6,7 @@
 
 #define AK_DONT_REPLACE_STD
 
+#include "../Utilities.h"
 #include <LibCore/ArgsParser.h>
 #include <LibCore/EventLoop.h>
 #include <LibCore/System.h>
@@ -36,6 +37,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         return 1;
     }
 
+    platform_init();
+
     Core::EventLoop loop;
     auto server = TRY(Core::TCPServer::try_create());