From 5fc51b2ff95ba30433d7274856213a790ccb610a Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 14 Sep 2024 13:02:44 -0400 Subject: [PATCH] WebContent+WebDriver: Update the current BC when switching windows We had only implemented this step in the WebDriver process, but we need to update the remote WebContent process as well. --- .../Services/WebContent/WebDriverClient.ipc | 2 +- .../WebContent/WebDriverConnection.cpp | 21 ++++++++++++++++++- .../Services/WebContent/WebDriverConnection.h | 2 +- Userland/Services/WebDriver/Session.cpp | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index dec8a3aede3..9b2f3b501c7 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -17,7 +17,7 @@ endpoint WebDriverClient { get_title() => (Web::WebDriver::Response response) get_window_handle() => (String handle) close_window() => (Web::WebDriver::Response response) - switch_to_window() => (Web::WebDriver::Response response) + switch_to_window(String handle) => (Web::WebDriver::Response response) new_window(JsonValue payload) => (Web::WebDriver::Response response) switch_to_frame(JsonValue payload) => (Web::WebDriver::Response response) switch_to_parent_frame(JsonValue payload) => (Web::WebDriver::Response response) diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 4b6e5c0692f..87065e1759a 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -523,8 +523,27 @@ Messages::WebDriverClient::CloseWindowResponse WebDriverConnection::close_window } // 11.3 Switch to Window, https://w3c.github.io/webdriver/#dfn-switch-to-window -Messages::WebDriverClient::SwitchToWindowResponse WebDriverConnection::switch_to_window() +Messages::WebDriverClient::SwitchToWindowResponse WebDriverConnection::switch_to_window(String const& handle) { + // 4. If handle is equal to the associated window handle for some top-level browsing context in the + // current session, let context be the that browsing context, and set the current top-level + // browsing context with context. + // Otherwise, return error with error code no such window. + bool found_matching_context = false; + + current_top_level_browsing_context()->for_each_in_inclusive_subtree([&](Web::HTML::BrowsingContext& context) { + if (handle != context.top_level_traversable()->window_handle()) + return Web::TraversalDecision::Continue; + + m_current_browsing_context = context; + found_matching_context = true; + + return Web::TraversalDecision::Break; + }); + + if (!found_matching_context) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"); + // 5. Update any implementation-specific state that would result from the user selecting the current // browsing context for interaction, without altering OS-level focus. current_browsing_context().page().client().page_did_request_activate_tab(); diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 306bcf75289..44b3f32b2a8 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -54,7 +54,7 @@ private: virtual Messages::WebDriverClient::GetTitleResponse get_title() override; virtual Messages::WebDriverClient::GetWindowHandleResponse get_window_handle() override; virtual Messages::WebDriverClient::CloseWindowResponse close_window() override; - virtual Messages::WebDriverClient::SwitchToWindowResponse switch_to_window() override; + virtual Messages::WebDriverClient::SwitchToWindowResponse switch_to_window(String const& handle) override; virtual Messages::WebDriverClient::NewWindowResponse new_window(JsonValue const& payload) override; virtual Messages::WebDriverClient::SwitchToFrameResponse switch_to_frame(JsonValue const& payload) override; virtual Messages::WebDriverClient::SwitchToParentFrameResponse switch_to_parent_frame(JsonValue const& payload) override; diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 65ae64f2cc0..68c6635c65b 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -148,7 +148,7 @@ Web::WebDriver::Response Session::switch_to_window(StringView handle) // 5. Update any implementation-specific state that would result from the user selecting the current // browsing context for interaction, without altering OS-level focus. - TRY(web_content_connection().switch_to_window()); + TRY(web_content_connection().switch_to_window(m_current_window_handle)); // 6. Return success with data null. return JsonValue {};