From 91326568565795702fadb50585550a3413e86428 Mon Sep 17 00:00:00 2001 From: Moustafa Raafat Date: Sat, 15 Oct 2022 18:15:38 +0100 Subject: [PATCH] WebDriver: Implement POST /session/{id}/back endpoint --- .../Browser/WebDriverConnection.cpp | 7 ++++++ .../Browser/WebDriverConnection.h | 1 + .../Browser/WebDriverSessionClient.ipc | 1 + Userland/Services/WebDriver/Client.cpp | 12 ++++++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 23 +++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 7 files changed, 46 insertions(+) diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 37854ff0db..67b4eb2514 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -53,4 +53,11 @@ void WebDriverConnection::refresh() browser_window->active_tab().reload(); } +void WebDriverConnection::back() +{ + dbgln("WebDriverConnection: back"); + if (auto browser_window = m_browser_window.strong_ref()) + browser_window->active_tab().go_back(); +} + } diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 0ba9bb7c79..5815012b8a 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -40,6 +40,7 @@ public: virtual void set_url(AK::URL const& url) override; virtual Messages::WebDriverSessionClient::GetTitleResponse get_title() override; virtual void refresh() override; + virtual void back() override; private: WebDriverConnection(NonnullOwnPtr socket, NonnullRefPtr browser_window); diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index e8644394a1..3d5b56d64d 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -7,4 +7,5 @@ endpoint WebDriverSessionClient { set_url(URL url) =| get_title() => (String title) refresh() =| + back() =| } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 25ea87bba4..7f4969f28e 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -27,6 +27,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_delete_window }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh }, + { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "back" }, &Client::handle_back }, }; Client::Client(NonnullOwnPtr socket, Core::Object* parent) @@ -463,4 +464,15 @@ ErrorOr Client::handle_refresh(Vector paramete return make_json_value(result); } +// POST /session/{session id}/back https://w3c.github.io/webdriver/#dfn-back +ErrorOr Client::handle_back(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//back"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::back(). + auto result = TRY(session->back()); + return make_json_value(result); +} + } diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index a5b5d8bcde..56a69817cd 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -54,6 +54,7 @@ private: ErrorOr handle_get_title(Vector, JsonValue const& payload); ErrorOr handle_delete_window(Vector, JsonValue const& payload); ErrorOr handle_refresh(Vector, JsonValue const& payload); + ErrorOr handle_back(Vector, JsonValue const& payload); ErrorOr find_session_with_id(StringView session_id); JsonValue make_json_value(JsonValue const&); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 0eb15c9955..7229dd3c87 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -183,4 +183,27 @@ ErrorOr Session::refresh() return JsonValue(); } +// POST /session/{session id}/back https://w3c.github.io/webdriver/#dfn-back +ErrorOr Session::back() +{ + // 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 { 404, "no such window", "Window not found" }; + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // 3. Traverse the history by a delta –1 for the current browsing context. + m_browser_connection->async_back(); + + // FIXME: 4. If the previous step completed results in a pageHide event firing, wait until pageShow event + // fires or for the session page load timeout milliseconds to pass, whichever occurs sooner. + + // FIXME: 5. If the previous step completed by the session page load timeout being reached, and user + // prompts have been handled, return error with error code timeout. + + // 6. Return success with data null. + return JsonValue(); +} + } diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index a334322958..c276d9514f 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -38,6 +38,7 @@ public: ErrorOr get_url(); ErrorOr get_title(); ErrorOr refresh(); + ErrorOr back(); private: NonnullRefPtr m_client;