diff --git a/Userland/Libraries/LibWeb/WebDriver/Client.cpp b/Userland/Libraries/LibWeb/WebDriver/Client.cpp index f279d7ee26..7d791f73e1 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Client.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Client.cpp @@ -73,6 +73,7 @@ static constexpr auto s_webdriver_endpoints = Array { ROUTE(POST, "/session/:session_id/elements"sv, find_elements), ROUTE(POST, "/session/:session_id/element/:element_id/element"sv, find_element_from_element), ROUTE(POST, "/session/:session_id/element/:element_id/elements"sv, find_elements_from_element), + ROUTE(POST, "/session/:session_id/shadow/:shadow_id/element"sv, find_element_from_shadow_root), ROUTE(GET, "/session/:session_id/element/active"sv, get_active_element), ROUTE(GET, "/session/:session_id/element/:element_id/shadow"sv, get_element_shadow_root), ROUTE(GET, "/session/:session_id/element/:element_id/selected"sv, is_element_selected), diff --git a/Userland/Libraries/LibWeb/WebDriver/Client.h b/Userland/Libraries/LibWeb/WebDriver/Client.h index 2ede64127c..78fb14c174 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Client.h +++ b/Userland/Libraries/LibWeb/WebDriver/Client.h @@ -61,6 +61,7 @@ public: virtual Response find_elements(Parameters parameters, JsonValue payload) = 0; virtual Response find_element_from_element(Parameters parameters, JsonValue payload) = 0; virtual Response find_elements_from_element(Parameters parameters, JsonValue payload) = 0; + virtual Response find_element_from_shadow_root(Parameters parameters, JsonValue payload) = 0; virtual Response get_active_element(Parameters parameters, JsonValue payload) = 0; virtual Response get_element_shadow_root(Parameters parameters, JsonValue payload) = 0; virtual Response is_element_selected(Parameters parameters, JsonValue payload) = 0; diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index 6f0cc67409..ed949f09ec 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -23,6 +23,7 @@ endpoint WebDriverClient { find_elements(JsonValue payload) => (Web::WebDriver::Response response) find_element_from_element(JsonValue payload, String element_id) => (Web::WebDriver::Response response) find_elements_from_element(JsonValue payload, String element_id) => (Web::WebDriver::Response response) + find_element_from_shadow_root(JsonValue payload, String shadow_id) => (Web::WebDriver::Response response) get_active_element() => (Web::WebDriver::Response response) get_element_shadow_root(String element_id) => (Web::WebDriver::Response response) is_element_selected(String element_id) => (Web::WebDriver::Response response) diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index acf43b4c05..2aa44746a3 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -181,6 +181,23 @@ static JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shado return object; } +// https://w3c.github.io/webdriver/#dfn-get-a-known-shadow-root +static ErrorOr get_known_shadow_root(StringView shadow_id) +{ + // NOTE: The whole concept of "known shadow roots" is not implemented yet. See get_or_create_a_shadow_root_reference(). + // For now the shadow root is only represented by its ID. + auto shadow_root = shadow_id.to_int(); + if (!shadow_root.has_value()) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Shadow ID is not an integer"); + + auto* node = Web::DOM::Node::from_id(*shadow_root); + + if (!node || !node->is_shadow_root()) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, String::formatted("Could not find shadow root with ID: {}", shadow_id)); + + return static_cast(node); +} + // https://w3c.github.io/webdriver/#dfn-scrolls-into-view static void scroll_element_into_view(Web::DOM::Element& element) { @@ -736,6 +753,39 @@ Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection:: return TRY(find(*start_node, *location_strategy, selector)); } +// 12.3.6 Find Element From Shadow Root, https://w3c.github.io/webdriver/#find-element-from-shadow-root +Messages::WebDriverClient::FindElementFromShadowRootResponse WebDriverConnection::find_element_from_shadow_root(JsonValue const& payload, String const& shadow_id) +{ + // 1. Let location strategy be the result of getting a property called "using". + auto location_strategy_string = TRY(get_property(payload, "using"sv)); + auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string); + + // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument. + if (!location_strategy.has_value()) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string)); + + // 3. Let selector be the result of getting a property called "value". + // 4. If selector is undefined, return error with error code invalid argument. + auto selector = TRY(get_property(payload, "value"sv)); + + // 5. If the current browsing context is no longer open, return error with error code no such window. + TRY(ensure_open_top_level_browsing_context()); + + // FIXME: 6. Handle any user prompts and return its value if it is an error. + + // 7. Let start node be the result of trying to get a known shadow root with url variable shadow id. + auto* start_node = TRY(get_known_shadow_root(shadow_id)); + + // 8. Let result be the value of trying to Find with start node, location strategy, and selector. + auto result = TRY(find(*start_node, *location_strategy, selector)); + + // 9. If result is empty, return error with error code no such element. Otherwise, return the first element of result. + if (result.is_empty()) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv); + + return result.take(0); +} + // 12.3.8 Get Active Element, https://w3c.github.io/webdriver/#get-active-element Messages::WebDriverClient::GetActiveElementResponse WebDriverConnection::get_active_element() { diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 00540d73e0..b0e42d74d6 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -58,6 +58,7 @@ private: virtual Messages::WebDriverClient::FindElementsResponse find_elements(JsonValue const& payload) override; virtual Messages::WebDriverClient::FindElementFromElementResponse find_element_from_element(JsonValue const& payload, String const& element_id) override; virtual Messages::WebDriverClient::FindElementsFromElementResponse find_elements_from_element(JsonValue const& payload, String const& element_id) override; + virtual Messages::WebDriverClient::FindElementFromShadowRootResponse find_element_from_shadow_root(JsonValue const& payload, String const& shadow_id) override; virtual Messages::WebDriverClient::GetActiveElementResponse get_active_element() override; virtual Messages::WebDriverClient::GetElementShadowRootResponse get_element_shadow_root(String const& element_id) override; virtual Messages::WebDriverClient::IsElementSelectedResponse is_element_selected(String const& element_id) override; diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 0229d72966..a024bf9906 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -359,6 +359,15 @@ Web::WebDriver::Response Client::find_elements_from_element(Web::WebDriver::Para return session->web_content_connection().find_elements_from_element(payload, parameters[1]); } +// 12.3.6 Find Element From Shadow Root, https://w3c.github.io/webdriver/#find-element-from-shadow-root +// POST /session/{session id}/shadow/{shadow id}/element +Web::WebDriver::Response Client::find_element_from_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//shadow//element"); + auto* session = TRY(find_session_with_id(parameters[0])); + return session->web_content_connection().find_element_from_shadow_root(payload, parameters[1]); +} + // 12.3.8 Get Active Element, https://w3c.github.io/webdriver/#get-active-element // GET /session/{session id}/element/active Web::WebDriver::Response Client::get_active_element(Web::WebDriver::Parameters parameters, JsonValue) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index a968a25314..12fda62d42 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -56,6 +56,7 @@ private: virtual Web::WebDriver::Response find_elements(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response find_element_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response find_elements_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override; + virtual Web::WebDriver::Response find_element_from_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response get_active_element(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response get_element_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response is_element_selected(Web::WebDriver::Parameters parameters, JsonValue payload) override;