Skip to content

Truly independent web browser

LibWeb+WebContent: Keep the WebDriver script timeout timer alive

Tim Ledbetter83167549274d
diff --git a/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
index b5a7acbcf9e..0a319b31d50 100644
--- a/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
+++ b/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
@@ -108,7 +108,7 @@ static void fire_completion_when_resolved(GC::Ref<WebIDL::Promise> promise, GC::
WebIDL::react_to_promise(promise, reaction_steps, reaction_steps);
}
-void execute_script(HTML::BrowsingContext const& browsing_context, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete)
+GC::Ref<GC::Timer> execute_script(HTML::BrowsingContext const& browsing_context, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete)
{
auto const* document = browsing_context.active_document();
auto& realm = document->relevant_settings_object().realm();
@@ -160,10 +160,12 @@ void execute_script(HTML::BrowsingContext const& browsing_context, String body,
// 9. Wait until promise is resolved, or timer's timeout fired flag is set, whichever occurs first.
fire_completion_when_resolved(promise, timer, on_complete);
+
+ return timer;
}
// https://w3c.github.io/webdriver/#execute-async-script
-void execute_async_script(HTML::BrowsingContext const& browsing_context, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete)
+GC::Ref<GC::Timer> execute_async_script(HTML::BrowsingContext const& browsing_context, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete)
{
auto const* document = browsing_context.active_document();
auto& realm = document->relevant_settings_object().realm();
@@ -251,6 +253,8 @@ void execute_async_script(HTML::BrowsingContext const& browsing_context, String
// 9. Wait until promise is resolved, or timer's timeout fired flag is set, whichever occurs first.
fire_completion_when_resolved(promise, timer, on_complete);
+
+ return timer;
}
}
diff --git a/Libraries/LibWeb/WebDriver/ExecuteScript.h b/Libraries/LibWeb/WebDriver/ExecuteScript.h
index edb78b76df9..f89049e63e9 100644
--- a/Libraries/LibWeb/WebDriver/ExecuteScript.h
+++ b/Libraries/LibWeb/WebDriver/ExecuteScript.h
@@ -25,7 +25,7 @@ struct ExecutionResult {
using OnScriptComplete = GC::Function<void(ExecutionResult)>;
-WEB_API void execute_script(HTML::BrowsingContext const&, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete);
-WEB_API void execute_async_script(HTML::BrowsingContext const&, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete);
+WEB_API GC::Ref<GC::Timer> execute_script(HTML::BrowsingContext const&, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete);
+WEB_API GC::Ref<GC::Timer> execute_async_script(HTML::BrowsingContext const&, String body, GC::RootVector<JS::Value> arguments, Optional<u64> const& timeout_ms, GC::Ref<OnScriptComplete> on_complete);
}
diff --git a/Services/WebContent/WebDriverConnection.cpp b/Services/WebContent/WebDriverConnection.cpp
index 25e681065d2..bce90e2447f 100644
--- a/Services/WebContent/WebDriverConnection.cpp
+++ b/Services/WebContent/WebDriverConnection.cpp
@@ -424,6 +424,7 @@ void WebDriverConnection::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_document_observer);
visitor.visit(m_navigation_observer);
visitor.visit(m_navigation_timer);
+ visitor.visit(m_script_timer);
}
// 10.2 Get Current URL, https://w3c.github.io/webdriver/#get-current-url
@@ -1978,7 +1979,7 @@ Web::WebDriver::Response WebDriverConnection::execute_script(JsonValue payload)
auto timeout_ms = m_timeouts_configuration.script_timeout;
// This handles steps 5 to 9 and produces the appropriate result type for the following steps.
- Web::WebDriver::execute_script(current_browsing_context(), move(body), move(arguments), timeout_ms, GC::create_function(GC::Heap::the(), [this, script_execution_id](Web::WebDriver::ExecutionResult result) {
+ m_script_timer = Web::WebDriver::execute_script(current_browsing_context(), move(body), move(arguments), timeout_ms, GC::create_function(GC::Heap::the(), [this, script_execution_id](Web::WebDriver::ExecutionResult result) {
dbgln_if(WEBDRIVER_DEBUG, "Executing script returned: {}", result.value);
handle_script_response(result, script_execution_id);
}));
@@ -2005,7 +2006,7 @@ Web::WebDriver::Response WebDriverConnection::execute_async_script(JsonValue pay
auto timeout_ms = m_timeouts_configuration.script_timeout;
// This handles steps 5 to 9 and produces the appropriate result type for the following steps.
- Web::WebDriver::execute_async_script(current_browsing_context(), move(body), move(arguments), timeout_ms, GC::create_function(GC::Heap::the(), [this, script_execution_id](Web::WebDriver::ExecutionResult result) {
+ m_script_timer = Web::WebDriver::execute_async_script(current_browsing_context(), move(body), move(arguments), timeout_ms, GC::create_function(GC::Heap::the(), [this, script_execution_id](Web::WebDriver::ExecutionResult result) {
dbgln_if(WEBDRIVER_DEBUG, "Executing async script returned: {}", result.value);
handle_script_response(result, script_execution_id);
}));
@@ -2019,6 +2020,7 @@ void WebDriverConnection::handle_script_response(Web::WebDriver::ExecutionResult
if (script_execution_id != m_current_script_execution_id)
return;
m_current_script_execution_id.clear();
+ m_script_timer = nullptr;
auto response = [&]() -> Web::WebDriver::Response {
switch (result.state) {
@@ -2685,6 +2687,7 @@ void WebDriverConnection::page_did_open_dialog(Badge<PageClient>)
// [[Value]]: null, [[Target]]: empty }, but continue to run the other steps of this algorithm in parallel.
if (m_current_script_execution_id.has_value()) {
m_current_script_execution_id.clear();
+ m_script_timer = nullptr;
driver_execution_complete(JsonValue {});
}
}
diff --git a/Services/WebContent/WebDriverConnection.h b/Services/WebContent/WebDriverConnection.h
index 6c0929f5131..c7436710a49 100644
--- a/Services/WebContent/WebDriverConnection.h
+++ b/Services/WebContent/WebDriverConnection.h
@@ -159,6 +159,7 @@ private:
size_t m_script_execution_id_counter { 0 };
Optional<size_t> m_current_script_execution_id;
+ GC::Ptr<GC::Timer> m_script_timer;
friend class ElementLocator;
GC::Ptr<ElementLocator> m_element_locator;
diff --git a/Tests/LibWebView/test-webdriver-session-history.py b/Tests/LibWebView/test-webdriver-session-history.py
index 82ca3dba23f..a69598a0918 100755
--- a/Tests/LibWebView/test-webdriver-session-history.py
+++ b/Tests/LibWebView/test-webdriver-session-history.py
@@ -173,6 +173,27 @@ class TestPageHandler(http.server.BaseHTTPRequestHandler):
)
return
+ if self.path == "/webdriver-script-timeout-navigation-target":
+ self.send_response(200)
+ self.send_header("Content-Type", "text/html")
+ self.end_headers()
+ self.wfile.write(
+ b"""<!doctype html>
+<p>Target</p>
+<script>
+let remainingRounds = 10;
+function allocate() {
+ const objects = [];
+ for (let i = 0; i < 500000; ++i)
+ objects.push({ i });
+ if (--remainingRounds > 0)
+ setTimeout(allocate, 0);
+}
+allocate();
+</script>"""
+ )
+ return
+
if self.path == "/webdriver-intercepted-navigation-click":
self.send_response(200)
self.send_header("Content-Type", "text/html")
@@ -2039,6 +2060,25 @@ def run_webdriver_navigation_to_user_prompt_test(webdriver_port, url):
request(webdriver_port, "DELETE", f"/session/{session_id}")
+def run_webdriver_script_timeout_after_navigation_test(webdriver_port, url_a, target_url):
+ session_id = create_session(webdriver_port)
+ try:
+ load_url_from_ui(webdriver_port, session_id, url_a)
+ request(webdriver_port, "POST", f"/session/{session_id}/timeouts", {"script": 2000})
+
+ status, payload, response_body = request_raw(
+ webdriver_port,
+ "POST",
+ f"/session/{session_id}/execute/async",
+ {"script": f"location.href = {json.dumps(target_url)};", "args": []},
+ )
+ value = payload.get("value")
+ if status != 500 or not isinstance(value, dict) or value.get("error") != "script timeout":
+ raise AssertionError(f"Expected the script to time out, got HTTP {status}: {response_body}")
+ finally:
+ request(webdriver_port, "DELETE", f"/session/{session_id}")
+
+
def run_failed_redirected_navigation_shows_failed_url_test(webdriver_port, page_port, url_a):
url_redirect_to_tls_failure = f"http://localhost:{page_port}/redirect-to-tls-failure"
url_tls_failure = f"https://127.0.0.1:{page_port}/tls-failure"
@@ -2080,6 +2120,11 @@ def run_self_contained_navigation_tests(webdriver_port, page_port, url_a):
webdriver_port,
f"http://localhost:{page_port}/webdriver-navigation-user-prompt",
)
+ run_webdriver_script_timeout_after_navigation_test(
+ webdriver_port,
+ url_a,
+ f"http://localhost:{page_port}/webdriver-script-timeout-navigation-target",
+ )
run_failed_redirected_navigation_shows_failed_url_test(webdriver_port, page_port, url_a)