diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-21 11:19:28 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-21 18:54:22 +0000 |
commit | 1fdce714837f0a979c1e9a04cc9a72eeddfdd76a (patch) | |
tree | c1872839e70980c6816644df6696a987527114a8 | |
parent | 297186c68a2e590e4209ac42bb7bb1ed4818c2c0 (diff) | |
download | serenity-1fdce714837f0a979c1e9a04cc9a72eeddfdd76a.zip |
WebContent: Implement WebDriver's implicit timeout for finding elements
-rw-r--r-- | Userland/Services/WebContent/WebDriverConnection.cpp | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 1ba881c335..36a8711ac0 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -1633,7 +1633,8 @@ Gfx::IntRect WebDriverConnection::iconify_the_window() // https://w3c.github.io/webdriver/#dfn-find ErrorOr<JsonArray, Web::WebDriver::Error> WebDriverConnection::find(StartNodeGetter&& start_node_getter, Web::WebDriver::LocationStrategy using_, StringView value) { - // FIXME: 1. Let end time be the current time plus the session implicit wait timeout. + // 1. Let end time be the current time plus the session implicit wait timeout. + auto end_time = Time::now_monotonic() + Time::from_milliseconds(static_cast<i64>(m_timeouts_configuration.implicit_wait_timeout)); // 2. Let location strategy be equal to using. auto location_strategy = using_; @@ -1641,22 +1642,38 @@ ErrorOr<JsonArray, Web::WebDriver::Error> WebDriverConnection::find(StartNodeGet // 3. Let selector be equal to value. auto selector = value; - // 4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector. - auto elements = Web::WebDriver::invoke_location_strategy(location_strategy, *TRY(start_node_getter()), selector); + ErrorOr<JS::GCPtr<Web::DOM::NodeList>, Web::WebDriver::Error> maybe_elements { nullptr }; - // 5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector. - if (elements.is_error()) - return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, String::formatted("The location strategy could not finish: {}", elements.error().message)); + auto try_to_find_element = [&]() -> decltype(maybe_elements) { + // 4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector. + auto elements = Web::WebDriver::invoke_location_strategy(location_strategy, *TRY(start_node_getter()), selector); - // FIXME: 6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step. + // 5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector. + if (elements.is_error()) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, String::formatted("The location strategy could not finish: {}", elements.error().message)); + + return elements.release_value(); + }; + + Web::Platform::EventLoopPlugin::the().spin_until([&]() { + maybe_elements = try_to_find_element(); + if (maybe_elements.is_error()) + return true; + + // 6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step. + return maybe_elements.value()->length() != 0 || Time::now_monotonic() >= end_time; + }); + + auto elements = TRY(maybe_elements); + VERIFY(elements); // 7. Let result be an empty JSON List. JsonArray result; - result.ensure_capacity(elements.value()->length()); + result.ensure_capacity(elements->length()); // 8. For each element in elements returned, append the web element reference object for element, to result. - for (size_t i = 0; i < elements.value()->length(); ++i) - result.append(web_element_reference_object(*elements.value()->item(i))); + for (size_t i = 0; i < elements->length(); ++i) + result.append(web_element_reference_object(*elements->item(i))); // 9. Return success with data result. return result; |