From 727e982c509c42248de5ceb67f660d3e1d9979a1 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 17 Oct 2022 16:19:45 +0200 Subject: [PATCH] WebDriver: Put endpoint functions in spec order --- Userland/Services/WebDriver/Client.cpp | 78 +++++++-------- Userland/Services/WebDriver/Client.h | 6 +- Userland/Services/WebDriver/Session.cpp | 124 ++++++++++++------------ Userland/Services/WebDriver/Session.h | 6 +- 4 files changed, 107 insertions(+), 107 deletions(-) diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index efddfa70ec..8369ba6ea7 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -25,11 +25,11 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::GET, { "status" }, &Client::handle_get_status }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "url" }, &Client::handle_post_url }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "url" }, &Client::handle_get_url }, - { 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 }, { 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::DELETE, { "session", ":session_id", "window" }, &Client::handle_delete_window }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie }, @@ -441,6 +441,42 @@ ErrorOr Client::handle_get_url(Vector paramete return make_json_value(result); } +// 10.3 Back, https://w3c.github.io/webdriver/#dfn-back +// POST /session/{session id}/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); +} + +// 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward +// POST /session/{session id}/forward +ErrorOr Client::handle_forward(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//forward"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::forward(). + auto result = TRY(session->forward()); + return make_json_value(result); +} + +// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh +// POST /session/{session id}/refresh +ErrorOr Client::handle_refresh(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//refresh"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::refresh(). + auto result = TRY(session->refresh()); + return make_json_value(result); +} + // 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title // GET /session/{session id}/title ErrorOr Client::handle_get_title(Vector parameters, JsonValue const&) @@ -467,42 +503,6 @@ ErrorOr Client::handle_delete_window(Vector pa return make_json_value(JsonValue()); } -// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh -// POST /session/{session id}/refresh -ErrorOr Client::handle_refresh(Vector parameters, JsonValue const&) -{ - dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//refresh"); - Session* session = TRY(find_session_with_id(parameters[0])); - - // NOTE: Spec steps handled in Session::refresh(). - auto result = TRY(session->refresh()); - return make_json_value(result); -} - -// 10.3 Back, https://w3c.github.io/webdriver/#dfn-back -// POST /session/{session id}/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); -} - -// 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward -// POST /session/{session id}/forward -ErrorOr Client::handle_forward(Vector parameters, JsonValue const&) -{ - dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//forward"); - Session* session = TRY(find_session_with_id(parameters[0])); - - // NOTE: Spec steps handled in Session::forward(). - auto result = TRY(session->forward()); - return make_json_value(result); -} - // 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies // GET /session/{session id}/cookie ErrorOr Client::handle_get_all_cookies(Vector parameters, JsonValue const&) diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index fca28d43de..8ee1b257c5 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -51,11 +51,11 @@ private: ErrorOr handle_get_status(Vector, JsonValue const& payload); ErrorOr handle_post_url(Vector, JsonValue const& payload); ErrorOr handle_get_url(Vector, JsonValue const& payload); - 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 handle_forward(Vector, JsonValue const& payload); + ErrorOr handle_refresh(Vector, JsonValue const& payload); + ErrorOr handle_get_title(Vector, JsonValue const& payload); + ErrorOr handle_delete_window(Vector, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector, JsonValue const& payload); ErrorOr handle_get_named_cookie(Vector, JsonValue const& payload); ErrorOr handle_add_cookie(Vector, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index a2d4be1015..ecb13788d2 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -71,28 +71,6 @@ ErrorOr Session::stop() return {}; } -// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window -ErrorOr> Session::delete_window() -{ - // 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 Variant(HttpError { 404, "no such window", "Window not found" }); - - // 2. Close the current top-level browsing context. - m_windows.remove(m_current_window_handle); - - // 3. If there are no more open top-level browsing contexts, then close the session. - if (m_windows.is_empty()) { - auto result = stop(); - if (result.is_error()) { - return Variant(result.release_error()); - } - } - - return {}; -} - // 10.1 Navigate To, https://w3c.github.io/webdriver/#dfn-navigate-to ErrorOr Session::post_url(JsonValue const& payload) { @@ -146,46 +124,6 @@ ErrorOr Session::get_url() return JsonValue(url); } -// 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title -ErrorOr Session::get_title() -{ - // 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. Let title be the initial value of the title IDL attribute of the current top-level browsing context's active document. - // 4. Return success with data title. - return JsonValue(m_browser_connection->get_title()); -} - -// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh -ErrorOr Session::refresh() -{ - // 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. Initiate an overridden reload of the current top-level browsing context’s active document. - m_browser_connection->async_refresh(); - - // FIXME: 4. If url is special except for file: - - // FIXME: 1. Try to wait for navigation to complete. - - // FIXME: 2. Try to run the post-navigation checks. - - // FIXME: 5. Set the current browsing context with current top-level browsing context. - - // 6. Return success with data null. - return JsonValue(); -} - // 10.3 Back, https://w3c.github.io/webdriver/#dfn-back ErrorOr Session::back() { @@ -232,6 +170,68 @@ ErrorOr Session::forward() return JsonValue(); } +// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh +ErrorOr Session::refresh() +{ + // 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. Initiate an overridden reload of the current top-level browsing context’s active document. + m_browser_connection->async_refresh(); + + // FIXME: 4. If url is special except for file: + + // FIXME: 1. Try to wait for navigation to complete. + + // FIXME: 2. Try to run the post-navigation checks. + + // FIXME: 5. Set the current browsing context with current top-level browsing context. + + // 6. Return success with data null. + return JsonValue(); +} + +// 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title +ErrorOr Session::get_title() +{ + // 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. Let title be the initial value of the title IDL attribute of the current top-level browsing context's active document. + // 4. Return success with data title. + return JsonValue(m_browser_connection->get_title()); +} + +// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window +ErrorOr> Session::delete_window() +{ + // 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 Variant(HttpError { 404, "no such window", "Window not found" }); + + // 2. Close the current top-level browsing context. + m_windows.remove(m_current_window_handle); + + // 3. If there are no more open top-level browsing contexts, then close the session. + if (m_windows.is_empty()) { + auto result = stop(); + if (result.is_error()) { + return Variant(result.release_error()); + } + } + + return {}; +} + // https://w3c.github.io/webdriver/#dfn-serialized-cookie static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index cf3d45696d..66a1a04d76 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -33,13 +33,13 @@ public: ErrorOr start(); ErrorOr stop(); - ErrorOr> delete_window(); ErrorOr post_url(JsonValue const& url); ErrorOr get_url(); - ErrorOr get_title(); - ErrorOr refresh(); ErrorOr back(); ErrorOr forward(); + ErrorOr refresh(); + ErrorOr get_title(); + ErrorOr> delete_window(); ErrorOr get_all_cookies(); ErrorOr get_named_cookie(String const& name); ErrorOr add_cookie(JsonValue const& payload);