1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:07:45 +00:00

Browser+WebContent+WebDriver: Move Find Elements From Element to WC

This also lets us remove the element location strategy and some
WebContent IPC from Browser/LibWebView now that they are unused.
This commit is contained in:
Timothy Flynn 2022-11-09 15:25:23 -05:00 committed by Linus Groh
parent 5a750dc58c
commit 560da56a1d
16 changed files with 31 additions and 271 deletions

View file

@ -607,14 +607,6 @@ void BrowserWindow::create_new_tab(URL url, bool activate)
return active_tab().view().take_screenshot();
};
new_tab.webdriver_endpoints().on_get_document_element = [this]() {
return active_tab().view().get_document_element();
};
new_tab.webdriver_endpoints().on_query_selector_all = [this](i32 start_node_id, String const& selector) {
return active_tab().view().query_selector_all(start_node_id, selector);
};
new_tab.webdriver_endpoints().on_scroll_element_into_view = [this](i32 element_id) {
active_tab().view().scroll_element_into_view(element_id);
};

View file

@ -136,28 +136,6 @@ void WebDriverConnection::update_cookie(Web::Cookie::Cookie const& cookie)
}
}
Messages::WebDriverSessionClient::GetDocumentElementResponse WebDriverConnection::get_document_element()
{
dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_document_element");
if (auto browser_window = m_browser_window.strong_ref()) {
auto& tab = browser_window->active_tab();
if (tab.webdriver_endpoints().on_get_document_element)
return { tab.webdriver_endpoints().on_get_document_element() };
}
return { {} };
}
Messages::WebDriverSessionClient::QuerySelectorAllResponse WebDriverConnection::query_selector_all(i32 start_node_id, String const& selector)
{
dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: query_selector_all");
if (auto browser_window = m_browser_window.strong_ref()) {
auto& tab = browser_window->active_tab();
if (tab.webdriver_endpoints().on_query_selector_all)
return { tab.webdriver_endpoints().on_query_selector_all(start_node_id, selector) };
}
return { {} };
}
void WebDriverConnection::scroll_element_into_view(i32 element_id)
{
dbgln("WebDriverConnection: scroll_element_into_view {}", element_id);

View file

@ -48,8 +48,6 @@ public:
virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override;
virtual void add_cookie(Web::Cookie::ParsedCookie const&) override;
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 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;

View file

@ -23,8 +23,6 @@ public:
WebDriverEndpoints() = default;
~WebDriverEndpoints() = default;
Function<Optional<i32>()> on_get_document_element;
Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all;
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;

View file

@ -24,8 +24,6 @@ endpoint WebDriverSessionClient {
get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)
add_cookie(Web::Cookie::ParsedCookie cookie) =|
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)
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)

View file

@ -554,16 +554,6 @@ OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries(
return client().get_session_storage_entries();
}
Optional<i32> OutOfProcessWebView::get_document_element()
{
return client().get_document_element();
}
Optional<Vector<i32>> OutOfProcessWebView::query_selector_all(i32 start_node_id, String const& selector)
{
return client().query_selector_all(start_node_id, selector);
}
void OutOfProcessWebView::scroll_element_into_view(i32 element_id)
{
return client().scroll_element_into_view(element_id);

View file

@ -63,8 +63,6 @@ public:
OrderedHashMap<String, String> get_local_storage_entries();
OrderedHashMap<String, String> get_session_storage_entries();
Optional<i32> get_document_element();
Optional<Vector<i32>> query_selector_all(i32 start_node_id, String const& selector);
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);

View file

@ -468,39 +468,6 @@ void ConnectionFromClient::js_console_request_messages(i32 start_index)
m_console_client->send_messages(start_index);
}
Messages::WebContentServer::GetDocumentElementResponse ConnectionFromClient::get_document_element()
{
auto* document = page().top_level_browsing_context().active_document();
if (!document)
return Optional<i32> {};
return { document->id() };
}
Messages::WebContentServer::QuerySelectorAllResponse ConnectionFromClient::query_selector_all(i32 start_node_id, String const& selector)
{
auto* start_node = Web::DOM::Node::from_id(start_node_id);
if (!start_node)
return Optional<Vector<i32>> {};
if (!start_node->is_element() && !start_node->is_document())
return Optional<Vector<i32>> {};
auto& start_element = verify_cast<Web::DOM::ParentNode>(*start_node);
auto result = start_element.query_selector_all(selector);
if (result.is_error())
return Optional<Vector<i32>> {};
auto element_list = result.release_value();
Vector<i32> return_list;
for (u32 i = 0; i < element_list->length(); i++) {
auto node = element_list->item(i);
return_list.append(node->id());
}
return { return_list };
}
static Optional<Web::DOM::Element&> find_element_by_id(i32 element_id)
{
auto* node = Web::DOM::Node::from_id(element_id);

View file

@ -83,8 +83,6 @@ private:
virtual void run_javascript(String const&) override;
virtual void js_console_request_messages(i32) override;
virtual Messages::WebContentServer::GetDocumentElementResponse get_document_element() override;
virtual Messages::WebContentServer::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) 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;

View file

@ -42,8 +42,6 @@ endpoint WebContentServer
js_console_input(String js_source) =|
js_console_request_messages(i32 start_index) =|
get_document_element() => (Optional<i32> node_id)
query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids)
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)

View file

@ -12,4 +12,5 @@ endpoint WebDriverClient {
find_element(JsonValue payload) => (Web::WebDriver::Response response)
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)
}

View file

@ -427,6 +427,34 @@ Messages::WebDriverClient::FindElementFromElementResponse WebDriverConnection::f
return make_success_response(result.at(0));
}
// 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element
Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection::find_elements_from_element(JsonValue const& payload, String const& element_id)
{
// 1. Let location strategy be the result of getting a property called "using".
auto location_strategy_string = TRY(get_property(payload, "using"sv));
auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_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 (!location_strategy.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
// 3. Let selector be the result of getting a property called "value".
// 4. If selector is undefined, return error with error code invalid argument.
auto selector = TRY(get_property(payload, "value"sv));
// 5. 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: 6. Handle any user prompts and return its value if it is an error.
// 7. Let start node be the result of trying to get a known connected element with url variable element id.
auto* start_node = TRY(get_known_connected_element(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 make_success_response(move(result));
}
// https://w3c.github.io/webdriver/#dfn-no-longer-open
ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context()
{

View file

@ -42,6 +42,7 @@ private:
virtual Messages::WebDriverClient::FindElementResponse find_element(JsonValue const& payload) override;
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;
ErrorOr<void, Web::WebDriver::Error> ensure_open_top_level_browsing_context();
void restore_the_window();

View file

@ -645,8 +645,7 @@ Web::WebDriver::Response Client::handle_find_elements_from_element(Vector<String
{
dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/element/<element_id>/elements");
auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->find_elements_from_element(payload, parameters[1]));
return make_json_value(result);
return session->web_content_connection().find_elements_from_element(payload, parameters[1]);
}
// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected

View file

@ -311,34 +311,6 @@ static JsonValue serialize_rect(Gfx::IntRect const& rect)
return serialized_rect;
}
// https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
static String get_or_create_a_web_element_reference(Session::LocalElement const& element)
{
// FIXME: 1. For each known element of the current browsing contexts list of known elements:
// FIXME: 1. If known element equals element, return success with known elements web element reference.
// FIXME: 2. Add element to the list of known elements of the current browsing context.
// FIXME: 3. Return success with the elements web element reference.
return String::formatted("{}", element.id);
}
// https://w3c.github.io/webdriver/#dfn-web-element-identifier
static const String web_element_identifier = "element-6066-11e4-a52e-4f735466cecf";
// https://w3c.github.io/webdriver/#dfn-web-element-reference-object
static JsonObject web_element_reference_object(Session::LocalElement const& element)
{
// 1. Let identifier be the web element identifier.
auto identifier = web_element_identifier;
// 2. Let reference be the result of get or create a web element reference given element.
auto reference = get_or_create_a_web_element_reference(element);
// 3. Return a JSON Object initialized with a property with name identifier and value reference.
JsonObject object;
object.set("name"sv, identifier);
object.set("value"sv, reference);
return object;
}
// https://w3c.github.io/webdriver/#dfn-get-a-known-connected-element
static ErrorOr<i32, Web::WebDriver::Error> get_known_connected_element(StringView element_id)
{
@ -351,146 +323,6 @@ static ErrorOr<i32, Web::WebDriver::Error> get_known_connected_element(StringVie
return maybe_element_id.release_value();
}
// https://w3c.github.io/webdriver/#dfn-find
ErrorOr<JsonArray, Web::WebDriver::Error> Session::find(Session::LocalElement const& start_node, StringView using_, StringView value)
{
// 1. Let end time be the current time plus the session implicit wait timeout.
auto end_time = Time::now_monotonic() + Time::from_milliseconds(static_cast<i64>(m_timeouts_configuration.implicit_wait_timeout));
// 2. Let location strategy be equal to using.
auto location_strategy = using_;
// 3. Let selector be equal to value.
auto selector = value;
// 4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector.
auto location_strategy_handler = s_locator_strategies.first_matching([&](LocatorStrategy const& match) { return match.name == location_strategy; });
if (!location_strategy_handler.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "No valid location strategy");
auto elements_or_error = (this->*location_strategy_handler.value().handler)(start_node, selector);
// 5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector.
if (elements_or_error.is_error())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, String::formatted("The location strategy could not finish: {}", elements_or_error.release_error().message));
auto elements = elements_or_error.release_value();
// FIXME: 6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step.
(void)end_time;
// 7. Let result be an empty JSON List.
auto result = JsonArray();
// 8. For each element in elements returned, append the web element reference object for element, to result.
for (auto const& element : elements) {
result.append(JsonValue(web_element_reference_object(element)));
}
// 9. Return success with data result.
return result;
}
// https://w3c.github.io/webdriver/#dfn-table-of-location-strategies
Vector<Session::LocatorStrategy> Session::s_locator_strategies = {
{ "css selector", &Session::locator_strategy_css_selectors },
{ "link text", &Session::locator_strategy_link_text },
{ "partial link text", &Session::locator_strategy_partial_link_text },
{ "tag name", &Session::locator_strategy_tag_name },
{ "xpath", &Session::locator_strategy_x_path },
};
// https://w3c.github.io/webdriver/#css-selectors
ErrorOr<Vector<Session::LocalElement>, Web::WebDriver::Error> Session::locator_strategy_css_selectors(Session::LocalElement const& start_node, StringView selector)
{
// 1. Let elements be the result of calling querySelectorAll() with start node as this and selector as the argument.
// If this causes an exception to be thrown, return error with error code invalid selector.
auto elements_ids = m_browser_connection->query_selector_all(start_node.id, selector);
if (!elements_ids.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, "query_selector_all returned failed!");
Vector<Session::LocalElement> elements;
for (auto id : elements_ids.release_value()) {
elements.append({ id });
}
// 2.Return success with data elements.
return elements;
}
// https://w3c.github.io/webdriver/#link-text
ErrorOr<Vector<Session::LocalElement>, Web::WebDriver::Error> Session::locator_strategy_link_text(Session::LocalElement const&, StringView)
{
// FIXME: Implement
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Not implemented: locator strategy link text");
}
// https://w3c.github.io/webdriver/#partial-link-text
ErrorOr<Vector<Session::LocalElement>, Web::WebDriver::Error> Session::locator_strategy_partial_link_text(Session::LocalElement const&, StringView)
{
// FIXME: Implement
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Not implemented: locator strategy partial link text");
}
// https://w3c.github.io/webdriver/#tag-name
ErrorOr<Vector<Session::LocalElement>, Web::WebDriver::Error> Session::locator_strategy_tag_name(Session::LocalElement const&, StringView)
{
// FIXME: Implement
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Not implemented: locator strategy tag name");
}
// https://w3c.github.io/webdriver/#xpath
ErrorOr<Vector<Session::LocalElement>, Web::WebDriver::Error> Session::locator_strategy_x_path(Session::LocalElement const&, StringView)
{
// FIXME: Implement
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Not implemented: locator strategy XPath");
}
// 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element
Web::WebDriver::Response Session::find_elements_from_element(JsonValue const& payload, StringView parameter_element_id)
{
if (!payload.is_object())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "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 Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "No property called 'using' present");
auto const& maybe_location_strategy = properties.get("using"sv);
if (!maybe_location_strategy.is_string())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "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 Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "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 Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "No property called 'value' present");
auto const& maybe_selector = properties.get("value"sv);
if (!maybe_selector.is_string())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "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.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 6. Handle any user prompts and return its value if it is an error.
// 7. Let start node 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));
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);
}
// 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)
{

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 find_elements_from_element(JsonValue const& payload, StringView parameter_element_id);
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);
@ -80,21 +79,6 @@ public:
private:
void delete_cookies(Optional<StringView> const& name = {});
ErrorOr<JsonArray, Web::WebDriver::Error> find(LocalElement const& start_node, StringView location_strategy, StringView selector);
using ElementLocationStrategyHandler = ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> (Session::*)(LocalElement const&, StringView);
struct LocatorStrategy {
String name;
ElementLocationStrategyHandler handler;
};
static Vector<LocatorStrategy> s_locator_strategies;
ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> locator_strategy_css_selectors(LocalElement const&, StringView);
ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> locator_strategy_link_text(LocalElement const&, StringView);
ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> locator_strategy_partial_link_text(LocalElement const&, StringView);
ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> locator_strategy_tag_name(LocalElement const&, StringView);
ErrorOr<Vector<LocalElement>, Web::WebDriver::Error> locator_strategy_x_path(LocalElement const&, StringView);
enum class ServerType {
Browser,