From 174248678e63547e4f52d36cf911ac0e1995c6a4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 2 Nov 2022 09:33:24 -0400 Subject: [PATCH] WebDriver+Browser: Implement `POST /session/{id}/window/rect` --- .../Browser/WebDriverConnection.cpp | 23 ++++++ .../Browser/WebDriverConnection.h | 3 + .../Browser/WebDriverSessionClient.ipc | 5 ++ Userland/Services/WebDriver/Client.cpp | 11 +++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 75 +++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 7 files changed, 119 insertions(+) diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index b162320569..48a12384a0 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -79,6 +79,29 @@ Messages::WebDriverSessionClient::GetWindowRectResponse WebDriverConnection::get return { {} }; } +void WebDriverConnection::restore_window() +{ + dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: restore_window"); + if (auto browser_window = m_browser_window.strong_ref()) { + browser_window->show(); + browser_window->move_to_front(); + } +} + +void WebDriverConnection::set_window_size(Gfx::IntSize const& size) +{ + dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: set_window_size {}", size); + if (auto browser_window = m_browser_window.strong_ref()) + browser_window->resize(size); +} + +void WebDriverConnection::set_window_position(Gfx::IntPoint const& position) +{ + dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: set_window_position {}", position); + if (auto browser_window = m_browser_window.strong_ref()) + browser_window->move_to(position); +} + 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 04853d1603..92cbf5643e 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -44,6 +44,9 @@ public: virtual void back() override; virtual void forward() override; virtual Messages::WebDriverSessionClient::GetWindowRectResponse get_window_rect() override; + virtual void restore_window() override; + virtual void set_window_size(Gfx::IntSize const&) override; + virtual void set_window_position(Gfx::IntPoint const&) 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 6c44b99527..733fbccd69 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include @@ -14,6 +16,9 @@ endpoint WebDriverSessionClient { back() =| forward() =| get_window_rect() => (Gfx::IntRect rect) + restore_window() =| + set_window_size(Gfx::IntSize size) =| + set_window_position(Gfx::IntPoint position) =| get_all_cookies() => (Vector cookies) get_named_cookie(String name) => (Optional cookie) add_cookie(Web::Cookie::ParsedCookie cookie) =| diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 913e9a737b..625c83c4f4 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -37,6 +37,7 @@ Vector Client::s_routes = { { 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", "window", "rect" }, &Client::handle_set_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 }, @@ -554,6 +555,16 @@ ErrorOr Client::handle_get_window_rect(Vector Client::handle_set_window_rect(Vector const& parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//window/rect"); + auto* session = TRY(find_session_with_id(parameters[0])); + auto result = TRY(session->set_window_rect(payload)); + 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 23b0f48341..23cf1bcceb 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -62,6 +62,7 @@ private: 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_set_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 5ee917f5de..957b3b6167 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -10,11 +10,14 @@ #include "Session.h" #include "BrowserConnection.h" #include "Client.h" +#include #include #include #include #include +#include #include +#include #include #include #include @@ -305,6 +308,78 @@ ErrorOr Session::get_window_rect() return serialize_window_rect(m_browser_connection->get_window_rect()); } +// 11.8.2 Set Window Rect, https://w3c.github.io/webdriver/#dfn-set-window-rect +ErrorOr Session::set_window_rect(JsonValue const& payload) +{ + if (!payload.is_object()) + return WebDriverError::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object"); + + auto const& properties = payload.as_object(); + + auto resolve_property = [](auto name, auto const* property, auto min, auto max) -> ErrorOr, WebDriverError> { + if (!property) + return Optional {}; + if (!property->is_number()) + return WebDriverError::from_code(ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a Number", name)); + + auto number = property->template to_number(); + + if (number < min) + return WebDriverError::from_code(ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, number, min)); + if (number > max) + return WebDriverError::from_code(ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, number, max)); + + return static_cast(number); + }; + + // 1. Let width be the result of getting a property named width from the parameters argument, else let it be null. + auto const* width_property = properties.get_ptr("width"sv); + + // 2. Let height be the result of getting a property named height from the parameters argument, else let it be null. + auto const* height_property = properties.get_ptr("height"sv); + + // 3. Let x be the result of getting a property named x from the parameters argument, else let it be null. + auto const* x_property = properties.get_ptr("x"sv); + + // 4. Let y be the result of getting a property named y from the parameters argument, else let it be null. + auto const* y_property = properties.get_ptr("y"sv); + + // 5. If width or height is neither null nor a Number from 0 to 2^31 − 1, return error with error code invalid argument. + auto width = TRY(resolve_property("width"sv, width_property, 0, NumericLimits::max())); + auto height = TRY(resolve_property("height"sv, height_property, 0, NumericLimits::max())); + + // 6. If x or y is neither null nor a Number from −(2^31) to 2^31 − 1, return error with error code invalid argument. + auto x = TRY(resolve_property("x"sv, x_property, NumericLimits::min(), NumericLimits::max())); + auto y = TRY(resolve_property("y"sv, y_property, NumericLimits::min(), NumericLimits::max())); + + // 7. If the remote end does not support the Set Window Rect command for the current top-level browsing context for any reason, return error with error code unsupported operation. + + // 8. 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: 9. Handle any user prompts and return its value if it is an error. + // FIXME: 10. Fully exit fullscreen. + + // 11. Restore the window. + m_browser_connection->async_restore_window(); + + // 11. If width and height are not null: + if (width.has_value() && height.has_value()) { + // a. Set the width, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to width. + // b. Set the height, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to height. + m_browser_connection->async_set_window_size(Gfx::IntSize { *width, *height }); + } + + // 12. If x and y are not null: + if (x.has_value() && y.has_value()) { + // a. Run the implementation-specific steps to set the position of the operating system level window containing the current top-level browsing context to the position given by the x and y coordinates. + m_browser_connection->async_set_window_position(Gfx::IntPoint { *x, *y }); + } + + // 14. 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 b221b3a964..4967d1087b 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -51,6 +51,7 @@ public: ErrorOr> close_window(); ErrorOr get_window_handles() const; ErrorOr get_window_rect(); + ErrorOr set_window_rect(JsonValue const& payload); ErrorOr find_element(JsonValue const& payload); ErrorOr find_elements(JsonValue const& payload); ErrorOr find_element_from_element(JsonValue const& payload, StringView parameter_element_id);