From 04ea3992e9cd954bb7202e94c2c5c54116d73136 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 10 Nov 2022 08:15:39 -0500 Subject: [PATCH] Browser+WebContent+WebDriver: Move Is Element Selected to WebContent --- .../Applications/Browser/BrowserWindow.cpp | 4 -- .../Browser/WebDriverConnection.cpp | 11 ------ .../Browser/WebDriverConnection.h | 1 - .../Applications/Browser/WebDriverEndpoints.h | 1 - .../Browser/WebDriverSessionClient.ipc | 1 - .../LibWebView/OutOfProcessWebView.cpp | 5 --- .../LibWebView/OutOfProcessWebView.h | 1 - .../WebContent/ConnectionFromClient.cpp | 23 ------------ .../WebContent/ConnectionFromClient.h | 1 - .../Services/WebContent/WebContentServer.ipc | 1 - .../Services/WebContent/WebDriverClient.ipc | 1 + .../WebContent/WebDriverConnection.cpp | 37 +++++++++++++++++++ .../Services/WebContent/WebDriverConnection.h | 1 + Userland/Services/WebDriver/Client.cpp | 3 +- Userland/Services/WebDriver/Session.cpp | 24 ------------ Userland/Services/WebDriver/Session.h | 1 - 16 files changed, 40 insertions(+), 76 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index c6513a9e09..028e6c6032 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -611,10 +611,6 @@ void BrowserWindow::create_new_tab(URL url, bool activate) active_tab().view().scroll_element_into_view(element_id); }; - 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 9c5abdf9fe..f88684279d 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -146,17 +146,6 @@ void WebDriverConnection::scroll_element_into_view(i32 element_id) } } -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 b395740163..53461f7405 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -49,7 +49,6 @@ public: virtual void add_cookie(Web::Cookie::ParsedCookie const&) override; virtual void update_cookie(Web::Cookie::Cookie const&) override; virtual void scroll_element_into_view(i32 element_id) 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 3129801586..7fa3325ff8 100644 --- a/Userland/Applications/Browser/WebDriverEndpoints.h +++ b/Userland/Applications/Browser/WebDriverEndpoints.h @@ -24,7 +24,6 @@ public: ~WebDriverEndpoints() = default; Function on_scroll_element_into_view; - Function on_is_element_selected; Function(i32 element_id, String const&)> on_get_element_attribute; Function(i32 element_id, String const&)> on_get_element_property; Function on_get_active_documents_type; diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 7837551ec4..4d1fef4cbd 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -25,7 +25,6 @@ endpoint WebDriverSessionClient { add_cookie(Web::Cookie::ParsedCookie cookie) =| update_cookie(Web::Cookie::Cookie cookie) =| scroll_element_into_view(i32 element_id) => () - is_element_selected(i32 element_id) => (bool selected) 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) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 1a3521db29..c16150747a 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -559,11 +559,6 @@ void OutOfProcessWebView::scroll_element_into_view(i32 element_id) return client().scroll_element_into_view(element_id); } -bool OutOfProcessWebView::is_element_selected(i32 element_id) -{ - return client().is_element_selected(element_id); -} - Optional OutOfProcessWebView::get_element_attribute(i32 element_id, String const& name) { return client().get_element_attribute(element_id, name); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index c4fa603cc5..f4e0db23bd 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -64,7 +64,6 @@ public: OrderedHashMap get_session_storage_entries(); void scroll_element_into_view(i32 element_id); - bool is_element_selected(i32 element_id); Optional get_element_attribute(i32 element_id, String const& name); Optional get_element_property(i32 element_id, String const& name); String get_active_documents_type(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 4191473d4b..b4c53e4114 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -28,8 +28,6 @@ #include #include #include -#include -#include #include #include #include @@ -497,27 +495,6 @@ void ConnectionFromClient::scroll_element_into_view(i32 element_id) element->scroll_into_view(options); } -Messages::WebContentServer::IsElementSelectedResponse ConnectionFromClient::is_element_selected(i32 element_id) -{ - auto element = find_element_by_id(element_id); - if (!element.has_value()) - return { false }; - - bool selected = false; - - if (is(*element)) { - auto& input = dynamic_cast(*element); - using enum Web::HTML::HTMLInputElement::TypeAttributeState; - - if (input.type_state() == Checkbox || input.type_state() == RadioButton) - selected = input.checked(); - } else if (is(*element)) { - selected = dynamic_cast(*element).selected(); - } - - return { selected }; -} - Messages::WebContentServer::GetElementAttributeResponse ConnectionFromClient::get_element_attribute(i32 element_id, String const& name) { auto element = find_element_by_id(element_id); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index 8f1c4e984c..7780a6f610 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -84,7 +84,6 @@ private: virtual void js_console_request_messages(i32) override; virtual void scroll_element_into_view(i32 element_id) override; - virtual Messages::WebContentServer::IsElementSelectedResponse is_element_selected(i32 element_id) override; virtual Messages::WebContentServer::GetElementAttributeResponse get_element_attribute(i32 element_id, String const& name) override; virtual Messages::WebContentServer::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override; virtual Messages::WebContentServer::GetActiveDocumentsTypeResponse get_active_documents_type() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 5cb82cf51e..43aa36332d 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -43,7 +43,6 @@ endpoint WebContentServer js_console_request_messages(i32 start_index) =| scroll_element_into_view(i32 element_id) => () - is_element_selected(i32 element_id) => (bool selected) get_element_attribute(i32 element_id, String name) => (Optional attribute) get_element_property(i32 element_id, String name) => (Optional property) get_active_documents_type() => (String type) diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index c0e70a2c91..2b465f65e7 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -13,4 +13,5 @@ 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) + 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 1938161f89..3600ab0409 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -455,6 +457,41 @@ Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection:: return make_success_response(move(result)); } +// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected +Messages::WebDriverClient::IsElementSelectedResponse WebDriverConnection::is_element_selected(String const& element_id) +{ + // 1. 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: 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 = TRY(get_known_connected_element(element_id)); + + // 4. Let selected be the value corresponding to the first matching statement: + bool selected = false; + + // element is an input element with a type attribute in the Checkbox- or Radio Button state + if (is(*element)) { + // -> The result of element’s checkedness. + auto& input = static_cast(*element); + using enum Web::HTML::HTMLInputElement::TypeAttributeState; + + if (input.type_state() == Checkbox || input.type_state() == RadioButton) + selected = input.checked(); + } + // element is an option element + else if (is(*element)) { + // -> The result of element’s selectedness. + selected = static_cast(*element).selected(); + } + // Otherwise + // -> False. + + // 5. Return success with data selected. + return make_success_response(selected); +} + // https://w3c.github.io/webdriver/#dfn-no-longer-open ErrorOr WebDriverConnection::ensure_open_top_level_browsing_context() { diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 0335c6d362..711d0b5213 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -43,6 +43,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::IsElementSelectedResponse is_element_selected(String const& element_id) override; ErrorOr ensure_open_top_level_browsing_context(); void restore_the_window(); diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index a513f8cb15..59b22d3410 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -654,8 +654,7 @@ Web::WebDriver::Response Client::handle_is_element_selected(Vector c { dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//element//selected"); auto* session = TRY(find_session_with_id(parameters[0])); - auto result = TRY(session->is_element_selected(parameters[1])); - return make_json_value(result); + return session->web_content_connection().is_element_selected(parameters[1]); } // 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 49b4517a79..d0d1181af8 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -323,30 +323,6 @@ static ErrorOr get_known_connected_element(StringVie return maybe_element_id.release_value(); } -// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected -Web::WebDriver::Response 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 JsonValue { selected }; -} - // 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute Web::WebDriver::Response 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 ee0f0ae168..4ea36307d4 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -58,7 +58,6 @@ public: Web::WebDriver::Response get_window_handle(); ErrorOr> close_window(); Web::WebDriver::Response get_window_handles() const; - Web::WebDriver::Response is_element_selected(StringView element_id); Web::WebDriver::Response get_element_attribute(JsonValue const& payload, StringView element_id, StringView name); Web::WebDriver::Response get_element_property(JsonValue const& payload, StringView element_id, StringView name); Web::WebDriver::Response get_element_css_value(JsonValue const& payload, StringView element_id, StringView property_name);