diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp index 5c56ea0161..b8611a25e0 100644 --- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp @@ -331,6 +331,10 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt auto& vm = window->vm(); auto start = MonotonicTime::now(); + auto has_timed_out = [&] { + return timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast(*timeout)); + }; + // AD-HOC: An execution context is required for Promise creation hooks. HTML::TemporaryExecutionContext execution_context { document->relevant_settings_object() }; @@ -381,7 +385,7 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt vm.custom_data()->spin_event_loop_until([&] { if (script_promise.state() != JS::Promise::State::Pending) return true; - if (timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast(*timeout))) + if (has_timed_out()) return true; return false; }); @@ -395,12 +399,22 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt WebIDL::reject_promise(realm, promise_capability, script_promise.result()); }(); - // FIXME: 6. If promise is still pending and session script timeout milliseconds is reached, return error with error code script timeout. - + // 6. If promise is still pending and session script timeout milliseconds is reached, return error with error code script timeout. vm.custom_data()->spin_event_loop_until([&] { + if (has_timed_out()) { + return true; + } + return promise->state() != JS::Promise::State::Pending; }); + if (has_timed_out()) { + auto error_object = JsonObject {}; + error_object.set("name", "Error"); + error_object.set("message", "script timeout"); + return { ExecuteScriptResultType::Timeout, move(error_object) }; + } + auto json_value_or_error = json_clone(realm, promise->result()); if (json_value_or_error.is_error()) { auto error_object = JsonObject {};