From e1fb64abad713f9996cc6c0db4e745cf4b5dbd2e Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Wed, 19 Oct 2022 13:52:17 +0200 Subject: [PATCH] WebDriver: Implement `POST /session/{id}/element/{id}/elements` --- Userland/Services/WebDriver/Client.cpp | 14 +++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 53 +++++++++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 4 files changed, 69 insertions(+) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 46369a7256..77a2cba5e3 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -35,6 +35,7 @@ Vector Client::s_routes = { { 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 }, + { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element", ":element_id", "elements" }, &Client::handle_find_elements_from_element }, { 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 }, @@ -565,6 +566,19 @@ ErrorOr Client::handle_find_element_from_element(Vector Client::handle_find_elements_from_element(Vector const& parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//element//elements"); + auto* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::find_elements_from_element(). + auto result = TRY(session->find_elements_from_element(payload, parameters[1])); + + 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 f2644eca6a..0e64072575 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -60,6 +60,7 @@ private: 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); + ErrorOr handle_find_elements_from_element(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 6c90a67665..253f1c937e 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -524,6 +524,59 @@ ErrorOr Session::find_element_from_element(JsonValue const return JsonValue(result.at(0)); } +// 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element +ErrorOr Session::find_elements_from_element(JsonValue const& payload, StringView parameter_element_id) +{ + if (!payload.is_object()) + return HttpError { 400, "invalid argument", "Payload is not a JSON object" }; + + auto const& properties = payload.as_object(); + // 1. Let location strategy be the result of getting a property called "using". + if (!properties.has("using"sv)) + return HttpError { 400, "invalid argument", "No property called 'using' present" }; + auto const& maybe_location_strategy = properties.get("using"sv); + if (!maybe_location_strategy.is_string()) + return HttpError { 400, "invalid argument", "Property 'using' is not a String" }; + + auto location_strategy = maybe_location_strategy.to_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 (!s_locator_strategies.first_matching([&](LocatorStrategy const& match) { return match.name == location_strategy; }).has_value()) + return HttpError { 400, "invalid argument", "No valid location strategy" }; + + // 3. Let selector be the result of getting a property called "value". + // 4. If selector is undefined, return error with error code invalid argument. + if (!properties.has("value"sv)) + return HttpError { 400, "invalid argument", "No property called 'value' present" }; + auto const& maybe_selector = properties.get("value"sv); + if (!maybe_selector.is_string()) + return HttpError { 400, "invalid argument", "Property 'value' is not a String" }; + + auto selector = maybe_selector.to_string(); + + // 5. If the current browsing context is no longer open, return error with error code no such window. + auto current_window = get_window_object(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // FIXME: 6. Handle any user prompts and return its value if it is an error. + + // FIXME: 7. Let start node 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(); + LocalElement start_node = { element_id }; + + // 8. Return the result of trying to Find with start node, location strategy, and selector. + auto result = TRY(find(start_node, location_strategy, selector)); + return JsonValue(result); +} + // 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 6b3fd0d88a..1d4e53ef68 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -46,6 +46,7 @@ public: ErrorOr find_element(JsonValue const& payload); ErrorOr find_elements(JsonValue const& payload); ErrorOr find_element_from_element(JsonValue const& payload, StringView parameter_element_id); + ErrorOr find_elements_from_element(JsonValue const& payload, StringView parameter_element_id); ErrorOr> close_window(); ErrorOr get_all_cookies(); ErrorOr get_named_cookie(String const& name);