diff options
author | martinfalisse <martinmotteditfalisse@gmail.com> | 2022-11-01 10:12:50 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-01 10:55:34 +0000 |
commit | 76da0c6c546a20bed1ff97b1c77a3e0ac7936680 (patch) | |
tree | 5323644984767838e66536ae8af1daa461f227fa /Userland | |
parent | 5ffff09e05043a6e3c831e863d3d6815fcef8462 (diff) | |
download | serenity-76da0c6c546a20bed1ff97b1c77a3e0ac7936680.zip |
WebDriver: Implement `GET /session/{id}/element/{id}/text` endpoint
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/Browser/WebDriverConnection.cpp | 11 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverConnection.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Browser/WebDriverSessionClient.ipc | 1 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Client.cpp | 11 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Client.h | 1 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.cpp | 23 | ||||
-rw-r--r-- | Userland/Services/WebDriver/Session.h | 1 |
7 files changed, 49 insertions, 0 deletions
diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 88a8609c98..71a4769b44 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -188,6 +188,17 @@ Messages::WebDriverSessionClient::GetComputedValueForElementResponse WebDriverCo return { "" }; } +Messages::WebDriverSessionClient::GetElementTextResponse WebDriverConnection::get_element_text(i32 element_id) +{ + dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_element_text"); + if (auto browser_window = m_browser_window.strong_ref()) { + auto& tab = browser_window->active_tab(); + if (tab.webdriver_endpoints().on_get_element_text) + return { tab.webdriver_endpoints().on_get_element_text(element_id) }; + } + return { "" }; +} + Messages::WebDriverSessionClient::GetElementTagNameResponse WebDriverConnection::get_element_tag_name(i32 element_id) { dbgln("WebDriverConnection: get_computed_value_for_element"); diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 8fef50a610..d6a47d4012 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -53,6 +53,7 @@ public: virtual Messages::WebDriverSessionClient::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override; virtual Messages::WebDriverSessionClient::GetActiveDocumentsTypeResponse get_active_documents_type() override; virtual Messages::WebDriverSessionClient::GetComputedValueForElementResponse get_computed_value_for_element(i32 element_id, String const& property_name) override; + virtual Messages::WebDriverSessionClient::GetElementTextResponse get_element_text(i32 element_id) override; virtual Messages::WebDriverSessionClient::GetElementTagNameResponse get_element_tag_name(i32 element_id) override; private: diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 4dcfbe22c6..16c27d6454 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -22,6 +22,7 @@ endpoint WebDriverSessionClient { get_element_property(i32 element_id, String name) => (Optional<String> property) get_active_documents_type() => (String type) get_computed_value_for_element(i32 element_id, String property_name) => (String computed_value) + get_element_text(i32 element_id) => (String text) get_element_tag_name(i32 element_id) => (String tag_name) } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 716ca8a6ee..d0957c0359 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -43,6 +43,7 @@ Vector<Client::Route> Client::s_routes = { { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "attribute", ":name" }, &Client::handle_get_element_attribute }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "property", ":name" }, &Client::handle_get_element_property }, { 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", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, @@ -612,6 +613,16 @@ ErrorOr<JsonValue, WebDriverError> Client::handle_get_element_css_value(Vector<S return make_json_value(result); } +// 12.4.5 Get Element Text, https://w3c.github.io/webdriver/#dfn-get-element-text +// GET /session/{session id}/element/{element id}/text +ErrorOr<JsonValue, WebDriverError> Client::handle_get_element_text(Vector<StringView> const& parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/text"); + auto* session = TRY(find_session_with_id(parameters[0])); + auto result = TRY(session->get_element_text(payload, parameters[1])); + return make_json_value(result); +} + // 12.4.6 Get Element Tag Name, https://w3c.github.io/webdriver/#dfn-get-element-tag-name // GET /session/{session id}/element/{element id}/name ErrorOr<JsonValue, WebDriverError> Client::handle_get_element_tag_name(Vector<StringView> const& parameters, JsonValue const& payload) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 3b948176cc..60593a69ad 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -68,6 +68,7 @@ private: ErrorOr<JsonValue, WebDriverError> handle_get_element_attribute(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_get_element_property(Vector<StringView> const&, JsonValue const& payload); 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_all_cookies(Vector<StringView> const&, JsonValue const& payload); ErrorOr<JsonValue, WebDriverError> handle_get_named_cookie(Vector<StringView> const&, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 594affebf1..f973e36dcd 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -711,6 +711,29 @@ ErrorOr<JsonValue, WebDriverError> Session::get_element_css_value(JsonValue cons return JsonValue(computed_value); } +// 12.4.5 Get Element Text, https://w3c.github.io/webdriver/#dfn-get-element-text +ErrorOr<JsonValue, WebDriverError> Session::get_element_text(JsonValue const&, 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. + + // FIXME: 3. Let element be the result of trying to get a known connected element with url variable element 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. Let rendered text be the result of performing implementation-specific steps whose result is exactly the + // same as the result of a Function.[[Call]](null, element) with bot.dom.getVisibleText as the this value. + auto rendered_text = m_browser_connection->get_element_text(element_id); + + // 5. Return success with data rendered text. + return JsonValue(rendered_text); +} + // 12.4.6 Get Element Tag Name, https://w3c.github.io/webdriver/#dfn-get-element-tag-name ErrorOr<JsonValue, WebDriverError> Session::get_element_tag_name(JsonValue const&, StringView parameter_element_id) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index 634933ceab..cb5fb0cd9b 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -57,6 +57,7 @@ public: ErrorOr<JsonValue, WebDriverError> get_element_attribute(JsonValue const& payload, StringView element_id, StringView name); ErrorOr<JsonValue, WebDriverError> get_element_property(JsonValue const& payload, StringView element_id, StringView name); 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_all_cookies(); ErrorOr<JsonValue, WebDriverError> get_named_cookie(String const& name); |