1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:17:35 +00:00

WebDriver: Implement POST /session/{id}/elements endpoint

This commit is contained in:
Tobias Christiansen 2022-10-19 13:21:38 +02:00 committed by Linus Groh
parent e6f71d5e05
commit 063bd663a7
4 changed files with 68 additions and 0 deletions

View file

@ -33,6 +33,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window" }, &Client::handle_get_window_handle }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "window" }, &Client::handle_get_window_handle },
{ HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_close_window }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_close_window },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element" }, &Client::handle_find_element }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element" }, &Client::handle_find_element },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "elements" }, &Client::handle_find_elements },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie },
@ -537,6 +538,19 @@ ErrorOr<JsonValue, HttpError> Client::handle_find_element(Vector<StringView> con
return make_json_value(result); return make_json_value(result);
} }
// 12.3.3 Find Elements, https://w3c.github.io/webdriver/#dfn-find-elements
// POST /session/{session id}/elements
ErrorOr<JsonValue, HttpError> Client::handle_find_elements(Vector<StringView> const& parameters, JsonValue const& payload)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/elements");
auto* session = TRY(find_session_with_id(parameters[0]));
// NOTE: Spec steps handled in Session::find_elements().
auto result = TRY(session->find_elements(payload));
return make_json_value(result);
}
// 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies // 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies
// GET /session/{session id}/cookie // GET /session/{session id}/cookie
ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> const& parameters, JsonValue const&) ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> const& parameters, JsonValue const&)

View file

@ -58,6 +58,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_get_window_handle(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_get_window_handle(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_close_window(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_close_window(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_find_element(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_find_element(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_find_elements(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_all_cookies(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_get_all_cookies(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_named_cookie(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_get_named_cookie(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_add_cookie(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_add_cookie(Vector<StringView> const&, JsonValue const& payload);

View file

@ -414,6 +414,58 @@ ErrorOr<JsonValue, HttpError> Session::find_element(JsonValue const& payload)
return JsonValue(result.at(0)); return JsonValue(result.at(0));
} }
// 12.3.3 Find Elements, https://w3c.github.io/webdriver/#dfn-find-elements
ErrorOr<JsonValue, HttpError> Session::find_elements(JsonValue const& payload)
{
if (!payload.is_object())
return HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "No property called 'using' present" };
auto const& maybe_location_strategy = properties.get("using"sv);
if (!maybe_location_strategy.is_string())
return HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "No property called 'value' present" };
auto const& maybe_selector = properties.get("value"sv);
if (!maybe_selector.is_string())
return HttpError { 400, "invalid argument", "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.
auto current_window = get_window_object();
if (!current_window.has_value())
return HttpError { 404, "no such window", "Window not found" };
// FIXME: 6. Handle any user prompts and return its value if it is an error.
// 7. Let start node be the current browsing contexts document element.
auto maybe_start_node_id = m_browser_connection->get_document_element();
// 8. If start node is null, return error with error code no such element.
if (!maybe_start_node_id.has_value())
return HttpError { 404, "no such element", "document element does not exist" };
auto start_node_id = maybe_start_node_id.release_value();
LocalElement start_node = { start_node_id };
// 9. 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);
}
// https://w3c.github.io/webdriver/#dfn-serialized-cookie // https://w3c.github.io/webdriver/#dfn-serialized-cookie
static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie) static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
{ {

View file

@ -44,6 +44,7 @@ public:
ErrorOr<JsonValue, HttpError> refresh(); ErrorOr<JsonValue, HttpError> refresh();
ErrorOr<JsonValue, HttpError> get_title(); ErrorOr<JsonValue, HttpError> get_title();
ErrorOr<JsonValue, HttpError> find_element(JsonValue const& payload); ErrorOr<JsonValue, HttpError> find_element(JsonValue const& payload);
ErrorOr<JsonValue, HttpError> find_elements(JsonValue const& payload);
ErrorOr<void, Variant<HttpError, Error>> close_window(); ErrorOr<void, Variant<HttpError, Error>> close_window();
ErrorOr<JsonValue, HttpError> get_all_cookies(); ErrorOr<JsonValue, HttpError> get_all_cookies();
ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name); ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name);