diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-03 17:05:47 -0400 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-11-03 19:40:43 -0400 |
commit | 2d75229192f780fa4df845add4dcee37f6caa0ce (patch) | |
tree | 4acb06bc600df9248b53d3be4c5a088541227573 /Userland | |
parent | 4067138702b68cbc5805ed7881e4c3bfdd688757 (diff) | |
download | serenity-2d75229192f780fa4df845add4dcee37f6caa0ce.zip |
WebDriver+Browser: Implement `GET /session/{id}/element/{id}/selected`
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/Browser/BrowserWindow.cpp | 4 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverConnection.cpp | 11 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverConnection.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverEndpoints.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverSessionClient.ipc | 1 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Client.cpp | 11 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Client.h | 1 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.cpp | 24 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.h | 1 |
9 files changed, 55 insertions, 0 deletions
diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index f2a957f28b..a0e7bd773b 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -607,6 +607,10 @@ void BrowserWindow::create_new_tab(URL url, bool activate) return active_tab().view().query_selector_all(start_node_id, selector); }; + new_tab.webdriver_endpoints().on_is_element_selected = [this](i32 element_id) { + return active_tab().view().is_element_selected(element_id); + }; + new_tab.webdriver_endpoints().on_get_element_attribute = [this](i32 element_id, String const& name) { return active_tab().view().get_element_attribute(element_id, name); }; diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 091d3cba45..1a72a41145 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -218,6 +218,17 @@ Messages::WebDriverSessionClient::QuerySelectorAllResponse WebDriverConnection:: return { {} }; } +Messages::WebDriverSessionClient::IsElementSelectedResponse WebDriverConnection::is_element_selected(i32 element_id) +{ + dbgln("WebDriverConnection: is_element_selected {}", element_id); + if (auto browser_window = m_browser_window.strong_ref()) { + auto& tab = browser_window->active_tab(); + if (tab.webdriver_endpoints().on_is_element_selected) + return { tab.webdriver_endpoints().on_is_element_selected(element_id) }; + } + return { false }; +} + Messages::WebDriverSessionClient::GetElementAttributeResponse WebDriverConnection::get_element_attribute(i32 element_id, String const& name) { dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_element_attribute"); diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 8f39e36db8..8caf3fdcb6 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -58,6 +58,7 @@ public: virtual void update_cookie(Web::Cookie::Cookie const&) override; virtual Messages::WebDriverSessionClient::GetDocumentElementResponse get_document_element() override; virtual Messages::WebDriverSessionClient::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override; + virtual Messages::WebDriverSessionClient::IsElementSelectedResponse is_element_selected(i32 element_id) 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; diff --git a/Userland/Applications/Browser/WebDriverEndpoints.h b/Userland/Applications/Browser/WebDriverEndpoints.h index 9410a61520..188d6e494e 100644 --- a/Userland/Applications/Browser/WebDriverEndpoints.h +++ b/Userland/Applications/Browser/WebDriverEndpoints.h @@ -25,6 +25,7 @@ public: Function<Optional<i32>()> on_get_document_element; Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all; + Function<bool(i32 element_id)> on_is_element_selected; Function<Optional<String>(i32 element_id, String const&)> on_get_element_attribute; Function<Optional<String>(i32 element_id, String const&)> on_get_element_property; Function<String()> on_get_active_documents_type; diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 6dac30564d..d45862c85c 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -31,6 +31,7 @@ endpoint WebDriverSessionClient { update_cookie(Web::Cookie::Cookie cookie) =| get_document_element() => (Optional<i32> document_element_id) query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids) + is_element_selected(i32 element_id) => (bool selected) get_element_attribute(i32 element_id, String name) => (Optional<String> atttibute) get_element_property(i32 element_id, String name) => (Optional<String> property) get_active_documents_type() => (String type) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 77cc5bb0b6..844a6316b9 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -45,6 +45,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", "selected" }, &Client::handle_is_element_selected }, { 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 }, @@ -641,6 +642,16 @@ ErrorOr<JsonValue, WebDriverError> Client::handle_find_elements_from_element(Vec return make_json_value(result); } +// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected +// GET /session/{session id}/element/{element id}/selected +ErrorOr<JsonValue, WebDriverError> Client::handle_is_element_selected(Vector<StringView> const& parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/selected"); + auto* session = TRY(find_session_with_id(parameters[0])); + auto result = TRY(session->is_element_selected(parameters[1])); + 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, WebDriverError> Client::handle_get_element_attribute(Vector<StringView> const& parameters, JsonValue const& payload) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 4ca56b8f89..8367c98f5e 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -70,6 +70,7 @@ private: ErrorOr<JsonValue, WebDriverError> handle_find_elements(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_find_element_from_element(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_find_elements_from_element(Vector<StringView> const&, JsonValue const& payload); + ErrorOr<JsonValue, WebDriverError> handle_is_element_selected(Vector<StringView> const& parameters, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_get_element_attribute(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_get_element_property(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_get_element_css_value(Vector<StringView> const&, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 64dca937b2..7912463718 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -758,6 +758,30 @@ ErrorOr<JsonValue, WebDriverError> Session::find_elements_from_element(JsonValue return JsonValue(result); } +// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected +ErrorOr<JsonValue, WebDriverError> Session::is_element_selected(StringView parameter_element_id) +{ + // 1. If the current 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: 2. Handle any user prompts and return its value if it is an error. + + // 3. Let element be the result of trying to get a known connected element with url variable element id. + auto element_id = TRY(get_known_connected_element(parameter_element_id)); + + // 4. Let selected be the value corresponding to the first matching statement: + // element is an input element with a type attribute in the Checkbox- or Radio Button state + // -> The result of element’s checkedness. + // element is an option element + // -> The result of element’s selectedness. + // Otherwise + // -> False. + auto selected = m_browser_connection->is_element_selected(element_id); + + // 5. Return success with data selected. + return selected; +} + // 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute ErrorOr<JsonValue, WebDriverError> Session::get_element_attribute(JsonValue const&, StringView parameter_element_id, StringView name) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index e51952ab0d..67f9cf68b5 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -59,6 +59,7 @@ public: ErrorOr<JsonValue, WebDriverError> find_elements(JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> find_element_from_element(JsonValue const& payload, StringView parameter_element_id); ErrorOr<JsonValue, WebDriverError> find_elements_from_element(JsonValue const& payload, StringView parameter_element_id); + ErrorOr<JsonValue, WebDriverError> is_element_selected(StringView element_id); ErrorOr<JsonValue, WebDriverError> get_element_attribute(JsonValue const& payload, StringView element_id, StringView name); ErrorOr<JsonValue, WebDriverError> get_element_property(JsonValue const& payload, StringView element_id, StringView name); ErrorOr<JsonValue, WebDriverError> get_element_css_value(JsonValue const& payload, StringView element_id, StringView property_name); |