Browser+WebContent+WebDriver: Move Is Element Selected to WebContent

This commit is contained in:
Timothy Flynn 2022-11-10 08:15:39 -05:00 committed by Linus Groh
parent 560da56a1d
commit 04ea3992e9
Notes: sideshowbarker 2024-07-17 04:37:43 +09:00
16 changed files with 40 additions and 76 deletions

View file

@ -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);
};

View file

@ -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");

View file

@ -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;

View file

@ -24,7 +24,6 @@ public:
~WebDriverEndpoints() = default;
Function<void(i32 element_id)> on_scroll_element_into_view;
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;

View file

@ -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<String> atttibute)
get_element_property(i32 element_id, String name) => (Optional<String> property)
get_active_documents_type() => (String type)

View file

@ -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<String> OutOfProcessWebView::get_element_attribute(i32 element_id, String const& name)
{
return client().get_element_attribute(element_id, name);

View file

@ -64,7 +64,6 @@ public:
OrderedHashMap<String, String> get_session_storage_entries();
void scroll_element_into_view(i32 element_id);
bool is_element_selected(i32 element_id);
Optional<String> get_element_attribute(i32 element_id, String const& name);
Optional<String> get_element_property(i32 element_id, String const& name);
String get_active_documents_type();

View file

@ -28,8 +28,6 @@
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Storage.h>
#include <LibWeb/HTML/Window.h>
@ -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<Web::HTML::HTMLInputElement>(*element)) {
auto& input = dynamic_cast<Web::HTML::HTMLInputElement&>(*element);
using enum Web::HTML::HTMLInputElement::TypeAttributeState;
if (input.type_state() == Checkbox || input.type_state() == RadioButton)
selected = input.checked();
} else if (is<Web::HTML::HTMLOptionElement>(*element)) {
selected = dynamic_cast<Web::HTML::HTMLOptionElement&>(*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);

View file

@ -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;

View file

@ -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<String> attribute)
get_element_property(i32 element_id, String name) => (Optional<String> property)
get_active_documents_type() => (String type)

View file

@ -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)
}

View file

@ -13,6 +13,8 @@
#include <AK/Vector.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/Timer.h>
@ -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<Web::HTML::HTMLInputElement>(*element)) {
// -> The result of elements checkedness.
auto& input = static_cast<Web::HTML::HTMLInputElement&>(*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<Web::HTML::HTMLOptionElement>(*element)) {
// -> The result of elements selectedness.
selected = static_cast<Web::HTML::HTMLOptionElement&>(*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<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context()
{

View file

@ -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<void, Web::WebDriver::Error> ensure_open_top_level_browsing_context();
void restore_the_window();

View file

@ -654,8 +654,7 @@ Web::WebDriver::Response Client::handle_is_element_selected(Vector<StringView> c
{
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);
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

View file

@ -323,30 +323,6 @@ static ErrorOr<i32, Web::WebDriver::Error> 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 elements checkedness.
// element is an option element
// -> The result of elements 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)
{

View file

@ -58,7 +58,6 @@ public:
Web::WebDriver::Response get_window_handle();
ErrorOr<void, Variant<Web::WebDriver::Error, Error>> 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);