Ver código fonte

WebDriver: Implement `GET /session/{session id}/window/handles` endpoint

Linus Groh 2 anos atrás
pai
commit
6641c99c80

+ 13 - 2
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)

+ 1 - 0
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);

+ 14 - 0
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)
 {

+ 1 - 0
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);