diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index ff2c324733..13bbb7f31b 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2022, Florent Castelli * Copyright (c) 2022, Sam Atkins * Copyright (c) 2022, Tobias Christiansen + * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -29,6 +30,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh }, { 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_delete_window }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "element" }, &Client::handle_find_element }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, @@ -491,6 +493,22 @@ ErrorOr Client::handle_get_title(Vector const& return make_json_value(result); } +// 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle +// GET /session/{session id}/window +ErrorOr Client::handle_get_window_handle(Vector const& parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//window"); + auto* session = TRY(find_session_with_id(parameters[0])); + + // 1. If the current top-level browsing context is no longer open, return error with error code no such window. + auto current_window = session->get_window_object(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // 2. Return success with data being the window handle associated with the current top-level browsing context. + return make_json_value(session->current_window_handle()); +} + // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window // DELETE /session/{session id}/window ErrorOr Client::handle_delete_window(Vector const& parameters, JsonValue const&) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 06d99749d6..797bea5ee2 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -55,6 +55,7 @@ private: ErrorOr handle_forward(Vector const&, JsonValue const& payload); ErrorOr handle_refresh(Vector const&, JsonValue const& payload); ErrorOr handle_get_title(Vector const&, JsonValue const& payload); + ErrorOr handle_get_window_handle(Vector const&, JsonValue const& payload); ErrorOr handle_delete_window(Vector const&, JsonValue const& payload); ErrorOr handle_find_element(Vector const&, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector const&, JsonValue const& payload);