summaryrefslogtreecommitdiff
path: root/Userland/Services/WebDriver
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-10-19 21:41:09 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-19 22:23:47 +0200
commit6641c99c806afec9b9c58c13a05291b8b3241289 (patch)
treeb4195cad0bcc8df4f791dd0c208d11a8a77a115f /Userland/Services/WebDriver
parent91cec51b991406893e16412b14f05aba2eb4c193 (diff)
downloadserenity-6641c99c806afec9b9c58c13a05291b8b3241289.zip
WebDriver: Implement `GET /session/{session id}/window/handles` endpoint
Diffstat (limited to 'Userland/Services/WebDriver')
-rw-r--r--Userland/Services/WebDriver/Client.cpp15
-rw-r--r--Userland/Services/WebDriver/Client.h1
-rw-r--r--Userland/Services/WebDriver/Session.cpp14
-rw-r--r--Userland/Services/WebDriver/Session.h1
4 files changed, 29 insertions, 2 deletions
diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp
index ebd813c62e..26bd4c4970 100644
--- a/Userland/Services/WebDriver/Client.cpp
+++ b/Userland/Services/WebDriver/Client.cpp
@@ -35,6 +35,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title },
{ 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::GET, { "session", ":session_id", "window", "handles" }, &Client::handle_get_window_handles },
{ 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::POST, { "session", ":session_id", "element", ":element_id", "element" }, &Client::handle_find_element_from_element },
@@ -431,7 +432,7 @@ ErrorOr<JsonValue, HttpError> Client::handle_get_status(Vector<StringView> const
// 9.1 Get Timeouts, https://w3c.github.io/webdriver/#dfn-get-timeouts
// GET /session/{session id}/timeouts
-ErrorOr<JsonValue, HttpError> Client::handle_get_timeouts(Vector<AK::StringView> const& parameters, AK::JsonValue const&)
+ErrorOr<JsonValue, HttpError> Client::handle_get_timeouts(Vector<StringView> const& parameters, JsonValue const&)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session id>/timeouts");
auto* session = TRY(find_session_with_id(parameters[0]));
@@ -441,7 +442,7 @@ ErrorOr<JsonValue, HttpError> Client::handle_get_timeouts(Vector<AK::StringView>
// 9.2 Set Timeouts, https://w3c.github.io/webdriver/#dfn-set-timeouts
// POST /session/{session id}/timeouts
-ErrorOr<JsonValue, HttpError> Client::handle_set_timeouts(Vector<AK::StringView> const& parameters, AK::JsonValue const& payload)
+ErrorOr<JsonValue, HttpError> Client::handle_set_timeouts(Vector<StringView> const& parameters, JsonValue const& payload)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session id>/timeouts");
auto* session = TRY(find_session_with_id(parameters[0]));
@@ -529,6 +530,16 @@ ErrorOr<JsonValue, HttpError> Client::handle_close_window(Vector<StringView> con
return make_json_value(JsonValue());
}
+// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
+// GET /session/{session id}/window/handles
+ErrorOr<JsonValue, HttpError> Client::handle_get_window_handles(Vector<StringView> const& parameters, JsonValue const&)
+{
+ dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window/handles");
+ auto* session = TRY(find_session_with_id(parameters[0]));
+ auto result = TRY(session->get_window_handles());
+ return make_json_value(result);
+}
+
// 12.3.2 Find Element, https://w3c.github.io/webdriver/#dfn-find-element
// POST /session/{session id}/element
ErrorOr<JsonValue, HttpError> Client::handle_find_element(Vector<StringView> const& parameters, JsonValue const& payload)
diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h
index 16c65f0dd1..630267ea20 100644
--- a/Userland/Services/WebDriver/Client.h
+++ b/Userland/Services/WebDriver/Client.h
@@ -60,6 +60,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_get_title(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_get_window_handles(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_find_element_from_element(Vector<StringView> const&, JsonValue const& payload);
diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp
index f953177dfb..306e7b1847 100644
--- a/Userland/Services/WebDriver/Session.cpp
+++ b/Userland/Services/WebDriver/Session.cpp
@@ -269,6 +269,20 @@ ErrorOr<void, Variant<HttpError, Error>> Session::close_window()
return {};
}
+// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
+ErrorOr<JsonValue, HttpError> Session::get_window_handles() const
+{
+ // 1. Let handles be a JSON List.
+ auto handles = JsonArray {};
+
+ // 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
+ for (auto const& window_handle : m_windows.keys())
+ handles.append(window_handle);
+
+ // 3. Return success with data handles.
+ return handles;
+}
+
// https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
static String get_or_create_a_web_element_reference(Session::LocalElement const& element)
{
diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h
index a35b7981c7..90e6af7fc6 100644
--- a/Userland/Services/WebDriver/Session.h
+++ b/Userland/Services/WebDriver/Session.h
@@ -48,6 +48,7 @@ public:
ErrorOr<JsonValue, HttpError> get_title();
ErrorOr<JsonValue, HttpError> get_window_handle();
ErrorOr<void, Variant<HttpError, Error>> close_window();
+ ErrorOr<JsonValue, HttpError> get_window_handles() const;
ErrorOr<JsonValue, HttpError> find_element(JsonValue const& payload);
ErrorOr<JsonValue, HttpError> find_elements(JsonValue const& payload);
ErrorOr<JsonValue, HttpError> find_element_from_element(JsonValue const& payload, StringView parameter_element_id);