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

WebDriver+Browser: Implement GET /session/{id}/element/{id}/rect

This commit is contained in:
Timothy Flynn 2022-11-03 12:53:27 -04:00 committed by Linus Groh
parent a490bca29b
commit 08c687ef20
9 changed files with 68 additions and 0 deletions

View file

@ -49,6 +49,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "css", ":property_name" }, &Client::handle_get_element_css_value },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "text" }, &Client::handle_get_element_text },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "name" }, &Client::handle_get_element_tag_name },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "rect" }, &Client::handle_get_element_rect },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "source" }, &Client::handle_get_source },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "execute", "sync" }, &Client::handle_execute_script },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "execute", "async" }, &Client::handle_execute_async_script },
@ -688,6 +689,16 @@ ErrorOr<JsonValue, WebDriverError> Client::handle_get_element_tag_name(Vector<St
return make_json_value(result);
}
// 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect
// GET /session/{session id}/element/{element id}/rect
ErrorOr<JsonValue, WebDriverError> Client::handle_get_element_rect(Vector<StringView> const& parameters, JsonValue const&)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/rect");
auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->get_element_rect(parameters[1]));
return make_json_value(result);
}
// 13.1 Get Page Source, https://w3c.github.io/webdriver/#dfn-get-page-source
// GET /session/{session id}/source
ErrorOr<JsonValue, WebDriverError> Client::handle_get_source(Vector<StringView> const& parameters, JsonValue const&)

View file

@ -74,6 +74,7 @@ private:
ErrorOr<JsonValue, WebDriverError> handle_get_element_css_value(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_get_element_text(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_get_element_tag_name(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_get_element_rect(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_get_source(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_execute_script(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_execute_async_script(Vector<StringView> const&, JsonValue const& payload);

View file

@ -900,6 +900,42 @@ ErrorOr<JsonValue, WebDriverError> Session::get_element_tag_name(JsonValue const
return JsonValue(qualified_name);
}
// 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect
ErrorOr<JsonValue, WebDriverError> Session::get_element_rect(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.
// NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference()
// For now the element is only represented by its ID
auto maybe_element_id = parameter_element_id.to_int();
if (!maybe_element_id.has_value())
return WebDriverError::from_code(ErrorCode::InvalidArgument, "Element ID is not an i32");
auto element_id = maybe_element_id.release_value();
// 4. Calculate the absolute position of element and let it be coordinates.
// 5. Let rect be elements bounding rectangle.
auto rect = m_browser_connection->get_element_rect(element_id);
// 6. Let body be a new JSON Object initialized with:
// "x"
// The first value of coordinates.
// "y"
// The second value of coordinates.
// "width"
// Value of rects width dimension.
// "height"
// Value of rects height dimension.
auto body = serialize_rect(rect);
// 7. Return success with data body.
return body;
}
// 13.1 Get Page Source, https://w3c.github.io/webdriver/#dfn-get-page-source
ErrorOr<JsonValue, WebDriverError> Session::get_source()
{

View file

@ -63,6 +63,7 @@ public:
ErrorOr<JsonValue, WebDriverError> get_element_css_value(JsonValue const& payload, StringView element_id, StringView property_name);
ErrorOr<JsonValue, WebDriverError> get_element_text(JsonValue const& payload, StringView element_id);
ErrorOr<JsonValue, WebDriverError> get_element_tag_name(JsonValue const& payload, StringView element_id);
ErrorOr<JsonValue, WebDriverError> get_element_rect(StringView element_id);
ErrorOr<JsonValue, WebDriverError> get_source();
ErrorOr<JsonValue, WebDriverError> execute_script(JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> execute_async_script(JsonValue const& payload);