From 096fe865c6f58e3ef877454523f056b9b080dcd2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 10 Oct 2022 11:45:21 +0100 Subject: [PATCH] WebDriver: Implement `GET /session/{id}/url` endpoint --- Userland/Services/WebDriver/Client.cpp | 8 +++++--- Userland/Services/WebDriver/Session.cpp | 17 +++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 64df123296..8daaa507c4 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -417,12 +417,14 @@ ErrorOr Client::handle_post_url(Vector paramet } // GET /session/{session id}/url https://w3c.github.io/webdriver/#dfn-get-current-url -ErrorOr Client::handle_get_url(Vector, JsonValue const&) +ErrorOr Client::handle_get_url(Vector parameters, JsonValue const&) { dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//url"); + Session* session = TRY(find_session_with_id(parameters[0])); - // FIXME: Implement the spec steps - return HttpError { 400, "", "" }; + // NOTE: Spec steps handled in Session::get_url(). + auto result = TRY(session->get_url()); + return make_json_value(result); } // GET /session/{session id}/title https://w3c.github.io/webdriver/#dfn-get-title diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 38eb5eb4b2..f12716e220 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -126,6 +126,23 @@ ErrorOr Session::post_url(JsonValue const& payload) return JsonValue(); } +// GET /session/{session id}/url https://w3c.github.io/webdriver/#dfn-get-current-url +ErrorOr Session::get_url() +{ + // 1. If the current top-level 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 { 400, "no such window", "Window not found" }; + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // 3. Let url be the serialization of the current top-level browsing context’s active document’s document URL. + auto url = m_browser_connection->get_url().to_string(); + + // 4. Return success with data url. + return JsonValue(url); +} + // GET /session/{session id}/title https://w3c.github.io/webdriver/#dfn-get-title ErrorOr Session::get_title() { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index 2e3ba08885..407702d988 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -35,6 +35,7 @@ public: ErrorOr stop(); ErrorOr> delete_window(); ErrorOr post_url(JsonValue const& url); + ErrorOr get_url(); ErrorOr get_title(); private: