summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Christiansen <tobyase@serenityos.org>2022-10-19 20:46:56 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-19 22:30:06 +0200
commite87456e38ff2d0a699da692572e6a96cc46d681d (patch)
tree81b03d56455d66679a155bf951d041cfb3a7d361
parent3db92885cd7e28a098e268f5f85faae1a139fe20 (diff)
downloadserenity-e87456e38ff2d0a699da692572e6a96cc46d681d.zip
WebDriver: Implement `GET /session/{id}/element/{id}/property/{name}`
-rw-r--r--Userland/Applications/Browser/WebDriverConnection.cpp11
-rw-r--r--Userland/Applications/Browser/WebDriverConnection.h1
-rw-r--r--Userland/Applications/Browser/WebDriverSessionClient.ipc1
-rw-r--r--Userland/Services/WebDriver/Client.cpp11
-rw-r--r--Userland/Services/WebDriver/Client.h1
-rw-r--r--Userland/Services/WebDriver/Session.cpp30
-rw-r--r--Userland/Services/WebDriver/Session.h1
7 files changed, 56 insertions, 0 deletions
diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp
index 6035253ff2..0fc82d4e7c 100644
--- a/Userland/Applications/Browser/WebDriverConnection.cpp
+++ b/Userland/Applications/Browser/WebDriverConnection.cpp
@@ -155,4 +155,15 @@ Messages::WebDriverSessionClient::GetElementAttributeResponse WebDriverConnectio
return { {} };
}
+Messages::WebDriverSessionClient::GetElementPropertyResponse WebDriverConnection::get_element_property(i32 element_id, String const& name)
+{
+ dbgln("WebDriverConnection: get_element_property");
+ if (auto browser_window = m_browser_window.strong_ref()) {
+ auto& tab = browser_window->active_tab();
+ if (tab.on_get_element_property)
+ return { tab.on_get_element_property(element_id, name) };
+ }
+ return { {} };
+}
+
}
diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h
index 6259329787..f4e1f62859 100644
--- a/Userland/Applications/Browser/WebDriverConnection.h
+++ b/Userland/Applications/Browser/WebDriverConnection.h
@@ -50,6 +50,7 @@ public:
virtual Messages::WebDriverSessionClient::GetDocumentElementResponse get_document_element() override;
virtual Messages::WebDriverSessionClient::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override;
virtual Messages::WebDriverSessionClient::GetElementAttributeResponse get_element_attribute(i32 element_id, String const& name) override;
+ virtual Messages::WebDriverSessionClient::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override;
private:
WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);
diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc
index ef07fb2ebc..ef237d9b66 100644
--- a/Userland/Applications/Browser/WebDriverSessionClient.ipc
+++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc
@@ -19,5 +19,6 @@ endpoint WebDriverSessionClient {
get_document_element() => (Optional<i32> document_element_id)
query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids)
get_element_attribute(i32 element_id, String name) => (Optional<String> atttibute)
+ get_element_property(i32 element_id, String name) => (Optional<String> property)
}
diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp
index 26bd4c4970..fce51d0016 100644
--- a/Userland/Services/WebDriver/Client.cpp
+++ b/Userland/Services/WebDriver/Client.cpp
@@ -41,6 +41,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element", ":element_id", "element" }, &Client::handle_find_element_from_element },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element", ":element_id", "elements" }, &Client::handle_find_elements_from_element },
{ 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", "cookie" }, &Client::handle_get_all_cookies },
{ 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 },
@@ -590,6 +591,16 @@ ErrorOr<JsonValue, HttpError> Client::handle_get_element_attribute(Vector<String
return make_json_value(result);
}
+// 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property
+// GET /session/{session id}/element/{element id}/property/{name}
+ErrorOr<JsonValue, HttpError> Client::handle_get_element_property(Vector<StringView> const& parameters, JsonValue const& payload)
+{
+ dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/property/<name>");
+ auto* session = TRY(find_session_with_id(parameters[0]));
+ auto result = TRY(session->get_element_property(payload, parameters[1], parameters[2]));
+ return make_json_value(result);
+}
+
// 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies
// GET /session/{session id}/cookie
ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView> const& parameters, JsonValue const&)
diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h
index 630267ea20..7c03e8d37b 100644
--- a/Userland/Services/WebDriver/Client.h
+++ b/Userland/Services/WebDriver/Client.h
@@ -66,6 +66,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_find_element_from_element(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_find_elements_from_element(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_element_attribute(Vector<StringView> const&, JsonValue const& payload);
+ ErrorOr<JsonValue, HttpError> handle_get_element_property(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_add_cookie(Vector<StringView> const&, JsonValue const& payload);
diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp
index 306e7b1847..e61f79d86c 100644
--- a/Userland/Services/WebDriver/Session.cpp
+++ b/Userland/Services/WebDriver/Session.cpp
@@ -663,6 +663,36 @@ ErrorOr<JsonValue, HttpError> Session::get_element_attribute(JsonValue const&, S
return JsonValue(result.release_value());
}
+// 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property
+ErrorOr<JsonValue, HttpError> Session::get_element_property(JsonValue const&, StringView parameter_element_id, StringView name)
+{
+ // 1. 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: 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.
+ // 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 HttpError { 400, "invalid argument", "Element ID is not an i32" };
+
+ auto element_id = maybe_element_id.release_value();
+
+ // 4. Let property be the result of calling the Object.[[GetProperty]](name) on element.
+ auto property = m_browser_connection->get_element_property(element_id, name);
+
+ // 5. Let result be the value of property if not undefined, or null.
+ if (!property.has_value())
+ return JsonValue();
+
+ // 6. Return success with data result.
+ return JsonValue(property.release_value());
+}
+
// https://w3c.github.io/webdriver/#dfn-serialized-cookie
static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
{
diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h
index 90e6af7fc6..0d9a2c3e3b 100644
--- a/Userland/Services/WebDriver/Session.h
+++ b/Userland/Services/WebDriver/Session.h
@@ -54,6 +54,7 @@ public:
ErrorOr<JsonValue, HttpError> find_element_from_element(JsonValue const& payload, StringView parameter_element_id);
ErrorOr<JsonValue, HttpError> find_elements_from_element(JsonValue const& payload, StringView parameter_element_id);
ErrorOr<JsonValue, HttpError> get_element_attribute(JsonValue const& payload, StringView element_id, StringView name);
+ ErrorOr<JsonValue, HttpError> get_element_property(JsonValue const& payload, StringView element_id, StringView name);
ErrorOr<JsonValue, HttpError> get_all_cookies();
ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name);
ErrorOr<JsonValue, HttpError> add_cookie(JsonValue const& payload);