1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:47:43 +00:00

LibWeb+WebContent+WebDriver: Implement Find Elements From Shadow Root

This commit is contained in:
Timothy Flynn 2022-11-14 19:54:17 -05:00 committed by Linus Groh
parent 6d9bd04233
commit 8e624c8f6d
7 changed files with 41 additions and 0 deletions

View file

@ -74,6 +74,7 @@ static constexpr auto s_webdriver_endpoints = Array {
ROUTE(POST, "/session/:session_id/element/:element_id/element"sv, find_element_from_element),
ROUTE(POST, "/session/:session_id/element/:element_id/elements"sv, find_elements_from_element),
ROUTE(POST, "/session/:session_id/shadow/:shadow_id/element"sv, find_element_from_shadow_root),
ROUTE(POST, "/session/:session_id/shadow/:shadow_id/elements"sv, find_elements_from_shadow_root),
ROUTE(GET, "/session/:session_id/element/active"sv, get_active_element),
ROUTE(GET, "/session/:session_id/element/:element_id/shadow"sv, get_element_shadow_root),
ROUTE(GET, "/session/:session_id/element/:element_id/selected"sv, is_element_selected),

View file

@ -62,6 +62,7 @@ public:
virtual Response find_element_from_element(Parameters parameters, JsonValue payload) = 0;
virtual Response find_elements_from_element(Parameters parameters, JsonValue payload) = 0;
virtual Response find_element_from_shadow_root(Parameters parameters, JsonValue payload) = 0;
virtual Response find_elements_from_shadow_root(Parameters parameters, JsonValue payload) = 0;
virtual Response get_active_element(Parameters parameters, JsonValue payload) = 0;
virtual Response get_element_shadow_root(Parameters parameters, JsonValue payload) = 0;
virtual Response is_element_selected(Parameters parameters, JsonValue payload) = 0;

View file

@ -24,6 +24,7 @@ endpoint WebDriverClient {
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)
find_element_from_shadow_root(JsonValue payload, String shadow_id) => (Web::WebDriver::Response response)
find_elements_from_shadow_root(JsonValue payload, String shadow_id) => (Web::WebDriver::Response response)
get_active_element() => (Web::WebDriver::Response response)
get_element_shadow_root(String element_id) => (Web::WebDriver::Response response)
is_element_selected(String element_id) => (Web::WebDriver::Response response)

View file

@ -786,6 +786,33 @@ Messages::WebDriverClient::FindElementFromShadowRootResponse WebDriverConnection
return result.take(0);
}
// 12.3.7 Find Elements From Shadow Root, https://w3c.github.io/webdriver/#find-elements-from-shadow-root
Messages::WebDriverClient::FindElementsFromShadowRootResponse WebDriverConnection::find_elements_from_shadow_root(JsonValue const& payload, String const& shadow_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 shadow root with url variable shadow id.
auto* start_node = TRY(get_known_shadow_root(shadow_id));
// 8. Return the result of trying to Find with start node, location strategy, and selector.
return TRY(find(*start_node, *location_strategy, selector));
}
// 12.3.8 Get Active Element, https://w3c.github.io/webdriver/#get-active-element
Messages::WebDriverClient::GetActiveElementResponse WebDriverConnection::get_active_element()
{

View file

@ -59,6 +59,7 @@ private:
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::FindElementFromShadowRootResponse find_element_from_shadow_root(JsonValue const& payload, String const& shadow_id) override;
virtual Messages::WebDriverClient::FindElementsFromShadowRootResponse find_elements_from_shadow_root(JsonValue const& payload, String const& shadow_id) override;
virtual Messages::WebDriverClient::GetActiveElementResponse get_active_element() override;
virtual Messages::WebDriverClient::GetElementShadowRootResponse get_element_shadow_root(String const& element_id) override;
virtual Messages::WebDriverClient::IsElementSelectedResponse is_element_selected(String const& element_id) override;

View file

@ -368,6 +368,15 @@ Web::WebDriver::Response Client::find_element_from_shadow_root(Web::WebDriver::P
return session->web_content_connection().find_element_from_shadow_root(payload, parameters[1]);
}
// 12.3.7 Find Elements From Shadow Root, https://w3c.github.io/webdriver/#find-elements-from-shadow-root
// POST /session/{session id}/shadow/{shadow id}/elements
Web::WebDriver::Response Client::find_elements_from_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/shadow/<shadow_id>/elements");
auto* session = TRY(find_session_with_id(parameters[0]));
return session->web_content_connection().find_elements_from_shadow_root(payload, parameters[1]);
}
// 12.3.8 Get Active Element, https://w3c.github.io/webdriver/#get-active-element
// GET /session/{session id}/element/active
Web::WebDriver::Response Client::get_active_element(Web::WebDriver::Parameters parameters, JsonValue)

View file

@ -57,6 +57,7 @@ private:
virtual Web::WebDriver::Response find_element_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response find_elements_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response find_element_from_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response find_elements_from_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response get_active_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response get_element_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response is_element_selected(Web::WebDriver::Parameters parameters, JsonValue payload) override;