diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 71a4769b44..b162320569 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -71,6 +71,14 @@ void WebDriverConnection::forward() browser_window->active_tab().go_forward(); } +Messages::WebDriverSessionClient::GetWindowRectResponse WebDriverConnection::get_window_rect() +{ + dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_window_rect"); + if (auto browser_window = m_browser_window.strong_ref()) + return { browser_window->rect() }; + return { {} }; +} + Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get_all_cookies() { dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_cookies"); diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index d6a47d4012..04853d1603 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -43,6 +43,7 @@ public: virtual void refresh() override; virtual void back() override; virtual void forward() override; + virtual Messages::WebDriverSessionClient::GetWindowRectResponse get_window_rect() override; virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override; virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override; virtual void add_cookie(Web::Cookie::ParsedCookie const&) override; diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 16c27d6454..6c44b99527 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -12,6 +13,7 @@ endpoint WebDriverSessionClient { refresh() =| back() =| forward() =| + get_window_rect() => (Gfx::IntRect rect) get_all_cookies() => (Vector cookies) get_named_cookie(String name) => (Optional cookie) add_cookie(Web::Cookie::ParsedCookie cookie) =| diff --git a/Userland/Services/WebDriver/CMakeLists.txt b/Userland/Services/WebDriver/CMakeLists.txt index 68f75d4b19..de733a88e5 100644 --- a/Userland/Services/WebDriver/CMakeLists.txt +++ b/Userland/Services/WebDriver/CMakeLists.txt @@ -18,4 +18,4 @@ set(GENERATED_SOURCES ) serenity_bin(WebDriver) -target_link_libraries(WebDriver PRIVATE LibCore LibHTTP LibMain LibIPC LibWeb) +target_link_libraries(WebDriver PRIVATE LibCore LibHTTP LibMain LibIPC LibWeb LibGfx) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 939db6b10e..913e9a737b 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -36,6 +36,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window" }, &Client::handle_get_window_handle }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_close_window }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window", "handles" }, &Client::handle_get_window_handles }, + { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window", "rect" }, &Client::handle_get_window_rect }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element" }, &Client::handle_find_element }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "elements" }, &Client::handle_find_elements }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element", ":element_id", "element" }, &Client::handle_find_element_from_element }, @@ -543,6 +544,16 @@ ErrorOr Client::handle_get_window_handles(Vector Client::handle_get_window_rect(Vector const& parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//window/rect"); + auto* session = TRY(find_session_with_id(parameters[0])); + auto result = TRY(session->get_window_rect()); + return make_json_value(result); +} + // 12.3.2 Find Element, https://w3c.github.io/webdriver/#dfn-find-element // POST /session/{session id}/element ErrorOr Client::handle_find_element(Vector const& parameters, JsonValue const& payload) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 60593a69ad..23b0f48341 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -61,6 +61,7 @@ private: ErrorOr handle_get_window_handle(Vector const&, JsonValue const& payload); ErrorOr handle_close_window(Vector const&, JsonValue const& payload); ErrorOr handle_get_window_handles(Vector const&, JsonValue const& payload); + ErrorOr handle_get_window_rect(Vector const&, JsonValue const& payload); ErrorOr handle_find_element(Vector const&, JsonValue const& payload); ErrorOr handle_find_elements(Vector const&, JsonValue const& payload); ErrorOr handle_find_element_from_element(Vector const&, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index f973e36dcd..5ee917f5de 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -281,6 +282,29 @@ ErrorOr Session::get_window_handles() const return handles; } +static JsonObject serialize_window_rect(Gfx::IntRect const& rect) +{ + JsonObject serialized_rect = {}; + serialized_rect.set("x", rect.x()); + serialized_rect.set("y", rect.y()); + serialized_rect.set("width", rect.width()); + serialized_rect.set("height", rect.height()); + + return serialized_rect; +} + +// 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect +ErrorOr Session::get_window_rect() +{ + // 1. If the current top-level browsing context is no longer open, return error with error code no such window. + TRY(check_for_open_top_level_browsing_context_or_return_error()); + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // 3. Return success with data set to the WindowRect object for the current top-level browsing context. + return serialize_window_rect(m_browser_connection->get_window_rect()); +} + // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference static String get_or_create_a_web_element_reference(Session::LocalElement const& element) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index cb5fb0cd9b..b221b3a964 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -50,6 +50,7 @@ public: ErrorOr get_window_handle(); ErrorOr> close_window(); ErrorOr get_window_handles() const; + ErrorOr get_window_rect(); ErrorOr find_element(JsonValue const& payload); ErrorOr find_elements(JsonValue const& payload); ErrorOr find_element_from_element(JsonValue const& payload, StringView parameter_element_id);