diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index ddcb4a11de6..3bb7a9a1b65 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -183,23 +183,15 @@ ErrorOr Session::ensure_current_window_handle_is_va return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv); } -Web::WebDriver::Response Session::execute_script(JsonValue payload, ScriptMode mode) const +template +static Web::WebDriver::Response perform_async_action(Handler& handler, Action&& action) { - ScopeGuard guard { [&]() { web_content_connection().on_script_executed = nullptr; } }; - Optional response; - web_content_connection().on_script_executed = [&](auto result) { - response = move(result); - }; - switch (mode) { - case ScriptMode::Sync: - TRY(web_content_connection().execute_script(move(payload))); - break; - case ScriptMode::Async: - TRY(web_content_connection().execute_async_script(move(payload))); - break; - } + ScopeGuard guard { [&]() { handler = nullptr; } }; + handler = [&](auto result) { response = move(result); }; + + TRY(action()); Core::EventLoop::current().spin_until([&]() { return response.has_value(); @@ -208,40 +200,31 @@ Web::WebDriver::Response Session::execute_script(JsonValue payload, ScriptMode m return response.release_value(); } +Web::WebDriver::Response Session::execute_script(JsonValue payload, ScriptMode mode) const +{ + return perform_async_action(web_content_connection().on_script_executed, [&]() { + switch (mode) { + case ScriptMode::Sync: + return web_content_connection().execute_script(move(payload)); + case ScriptMode::Async: + return web_content_connection().execute_async_script(move(payload)); + } + VERIFY_NOT_REACHED(); + }); +} + Web::WebDriver::Response Session::element_click(String element_id) const { - ScopeGuard guard { [&]() { web_content_connection().on_actions_performed = nullptr; } }; - - Optional response; - web_content_connection().on_actions_performed = [&](auto result) { - response = move(result); - }; - - TRY(web_content_connection().element_click(move(element_id))); - - Core::EventLoop::current().spin_until([&]() { - return response.has_value(); + return perform_async_action(web_content_connection().on_actions_performed, [&]() { + return web_content_connection().element_click(move(element_id)); }); - - return response.release_value(); } Web::WebDriver::Response Session::perform_actions(JsonValue payload) const { - ScopeGuard guard { [&]() { web_content_connection().on_actions_performed = nullptr; } }; - - Optional response; - web_content_connection().on_actions_performed = [&](auto result) { - response = move(result); - }; - - TRY(web_content_connection().perform_actions(move(payload))); - - Core::EventLoop::current().spin_until([&]() { - return response.has_value(); + return perform_async_action(web_content_connection().on_actions_performed, [&]() { + return web_content_connection().perform_actions(move(payload)); }); - - return response.release_value(); } }