From 354a845d6586f6ef6b5ab49a645d7dcdf0354860 Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Thu, 20 Oct 2022 12:06:52 +0200 Subject: [PATCH] WebDriver: Implement `GET /session/{id}/element/{id}/css/{name}` --- .../Browser/WebDriverConnection.cpp | 22 ++++++++++++ .../Browser/WebDriverConnection.h | 2 ++ .../Browser/WebDriverSessionClient.ipc | 2 ++ Userland/Services/WebDriver/Client.cpp | 11 ++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 34 +++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 7 files changed, 73 insertions(+) diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 0fc82d4e7c..c7bf1aa65f 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -166,4 +166,26 @@ Messages::WebDriverSessionClient::GetElementPropertyResponse WebDriverConnection return { {} }; } +Messages::WebDriverSessionClient::GetActiveDocumentsTypeResponse WebDriverConnection::get_active_documents_type() +{ + dbgln("WebDriverConnection: get_active_documents_type"); + if (auto browser_window = m_browser_window.strong_ref()) { + auto& tab = browser_window->active_tab(); + if (tab.on_get_active_documents_type) + return { tab.on_get_active_documents_type() }; + } + return { "" }; +} + +Messages::WebDriverSessionClient::GetComputedValueForElementResponse WebDriverConnection::get_computed_value_for_element(i32 element_id, String const& property_name) +{ + dbgln("WebDriverConnection: get_computed_value_for_element"); + if (auto browser_window = m_browser_window.strong_ref()) { + auto& tab = browser_window->active_tab(); + if (tab.on_get_computed_value_for_element) + return { tab.on_get_computed_value_for_element(element_id, property_name) }; + } + return { "" }; +} + } diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index f4e1f62859..9c32bf43c7 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -51,6 +51,8 @@ public: virtual Messages::WebDriverSessionClient::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override; virtual Messages::WebDriverSessionClient::GetElementAttributeResponse get_element_attribute(i32 element_id, String const& name) override; virtual Messages::WebDriverSessionClient::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override; + virtual Messages::WebDriverSessionClient::GetActiveDocumentsTypeResponse get_active_documents_type() override; + virtual Messages::WebDriverSessionClient::GetComputedValueForElementResponse get_computed_value_for_element(i32 element_id, String const& property_name) override; private: WebDriverConnection(NonnullOwnPtr socket, NonnullRefPtr browser_window); diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index ef237d9b66..4896650c76 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -20,5 +20,7 @@ endpoint WebDriverSessionClient { query_selector_all(i32 start_node_id, String selector) => (Optional> elements_ids) get_element_attribute(i32 element_id, String name) => (Optional atttibute) get_element_property(i32 element_id, String name) => (Optional property) + get_active_documents_type() => (String type) + get_computed_value_for_element(i32 element_id, String property_name) => (String computed_value) } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index fce51d0016..0fef70ae60 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -42,6 +42,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element", ":element_id", "elements" }, &Client::handle_find_elements_from_element }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "attribute", ":name" }, &Client::handle_get_element_attribute }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "property", ":name" }, &Client::handle_get_element_property }, + { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "css", ":property_name" }, &Client::handle_get_element_css_value }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie }, @@ -601,6 +602,16 @@ ErrorOr Client::handle_get_element_property(Vector Client::handle_get_element_css_value(Vector const& parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//element//css/"); + auto* session = TRY(find_session_with_id(parameters[0])); + auto result = TRY(session->get_element_css_value(payload, parameters[1], parameters[2])); + return make_json_value(result); +} + // 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies // GET /session/{session id}/cookie ErrorOr Client::handle_get_all_cookies(Vector const& parameters, JsonValue const&) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 7c03e8d37b..c7cd768588 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -67,6 +67,7 @@ private: ErrorOr handle_find_elements_from_element(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_attribute(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_property(Vector const&, JsonValue const& payload); + ErrorOr handle_get_element_css_value(Vector const&, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector const&, JsonValue const& payload); ErrorOr handle_get_named_cookie(Vector const&, JsonValue const& payload); ErrorOr handle_add_cookie(Vector const&, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index b854b5bc12..5107e142d7 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -693,6 +693,40 @@ ErrorOr Session::get_element_property(JsonValue const&, St return JsonValue(property.release_value()); } +// 12.4.4 Get Element CSS Value, https://w3c.github.io/webdriver/#dfn-get-element-css-value +ErrorOr Session::get_element_css_value(JsonValue const&, StringView parameter_element_id, StringView property_name) +{ + // 1. If the current browsing context is no longer open, return error with error code no such window. + auto current_window = this->current_window(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // FIXME: 3. Let element be the result of trying to get a known connected element with url variable element id. + // NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference() + // For now the element is only represented by its ID + auto maybe_element_id = parameter_element_id.to_int(); + if (!maybe_element_id.has_value()) + return HttpError { 400, "invalid argument", "Element ID is not an i32" }; + + auto element_id = maybe_element_id.release_value(); + + // 4. Let computed value be the result of the first matching condition: + // -> current browsing context’s active document’s type is not "xml" + // computed value of parameter property name from element’s style declarations. property name is obtained from url variables. + // -> Otherwise + // "" (empty string) + auto active_documents_type = m_browser_connection->get_active_documents_type(); + if (active_documents_type == "xml") + return JsonValue(""); + + auto computed_value = m_browser_connection->get_computed_value_for_element(element_id, property_name); + + // 5. Return success with data computed value. + return JsonValue(computed_value); +} + // https://w3c.github.io/webdriver/#dfn-serialized-cookie static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index 0d9a2c3e3b..36f2474fc6 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -55,6 +55,7 @@ public: ErrorOr find_elements_from_element(JsonValue const& payload, StringView parameter_element_id); ErrorOr get_element_attribute(JsonValue const& payload, StringView element_id, StringView name); ErrorOr get_element_property(JsonValue const& payload, StringView element_id, StringView name); + ErrorOr get_element_css_value(JsonValue const& payload, StringView element_id, StringView property_name); ErrorOr get_all_cookies(); ErrorOr get_named_cookie(String const& name); ErrorOr add_cookie(JsonValue const& payload);