diff options
author | Tobias Christiansen <tobyase@serenityos.org> | 2022-10-19 16:58:14 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-10-19 17:30:58 +0200 |
commit | 249350a7a31b332911a40050d3f08d99b9bc1dc7 (patch) | |
tree | 58df0f12e51e51acd33dd9375d8f7845c3434cf7 /Userland/Services | |
parent | 3f5a620b5d0e90e548c2854905ed656b34002ca5 (diff) | |
download | serenity-249350a7a31b332911a40050d3f08d99b9bc1dc7.zip |
WebDriver: Implement `GET /session/{id}/element/{id}/attribute/{name}`
Diffstat (limited to 'Userland/Services')
-rw-r--r-- | Userland/Services/WebDriver/Client.cpp | 14 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Client.h | 1 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.cpp | 36 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.h | 1 |
4 files changed, 52 insertions, 0 deletions
diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 77a2cba5e3..8f277f38c1 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -36,6 +36,7 @@ Vector<Client::Route> Client::s_routes = { { 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", "element", ":element_id", "attribute", ":name" }, &Client::handle_get_element_attribute }, { 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 }, @@ -579,6 +580,19 @@ ErrorOr<JsonValue, HttpError> Client::handle_find_elements_from_element(Vector<S return make_json_value(result); } +// 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute +// GET /session/{session id}/element/{element id}/attribute/{name} +ErrorOr<JsonValue, HttpError> Client::handle_get_element_attribute(Vector<StringView> const& parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/attribute/<name>"); + auto* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::get_element_attribute(). + auto result = TRY(session->get_element_attribute(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<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> const& parameters, JsonValue const&) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 0e64072575..09f8d7b2f2 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -61,6 +61,7 @@ private: ErrorOr<JsonValue, HttpError> handle_find_elements(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_find_element_from_element(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_find_elements_from_element(Vector<StringView> const&, JsonValue const& payload); + ErrorOr<JsonValue, HttpError> handle_get_element_attribute(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_get_all_cookies(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_get_named_cookie(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_add_cookie(Vector<StringView> const&, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 253f1c937e..cc6615bca3 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -577,6 +577,42 @@ ErrorOr<JsonValue, HttpError> Session::find_elements_from_element(JsonValue cons return JsonValue(result); } +// 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute +ErrorOr<JsonValue, HttpError> Session::get_element_attribute(JsonValue const&, StringView parameter_element_id, StringView name) +{ + // 1. 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: 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(); + + // FIXME: The case that the element does not exist is not handled at all and null is returned in that case. + + // 4. Let result be the result of the first matching condition: + // -> FIXME: If name is a boolean attribute + // NOTE: LibWeb doesn't know about boolean attributes directly + // "true" (string) if the element has the attribute, otherwise null. + // -> Otherwise + // The result of getting an attribute by name name. + auto result = m_browser_connection->get_element_attribute(element_id, name); + + if (!result.has_value()) + return JsonValue(AK::JsonValue::Type::Null); + + // 5. Return success with data result. + return JsonValue(result.release_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 1d4e53ef68..84ef798e7f 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -47,6 +47,7 @@ public: ErrorOr<JsonValue, HttpError> find_elements(JsonValue const& payload); ErrorOr<JsonValue, HttpError> find_element_from_element(JsonValue const& payload, StringView parameter_element_id); ErrorOr<JsonValue, HttpError> find_elements_from_element(JsonValue const& payload, StringView parameter_element_id); + ErrorOr<JsonValue, HttpError> get_element_attribute(JsonValue const& payload, StringView element_id, StringView name); ErrorOr<void, Variant<HttpError, Error>> close_window(); ErrorOr<JsonValue, HttpError> get_all_cookies(); ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name); |