diff --git a/Userland/Libraries/LibWeb/WebDriver/ElementReference.cpp b/Userland/Libraries/LibWeb/WebDriver/ElementReference.cpp index 2825c5b5122..8c21a4f5b8a 100644 --- a/Userland/Libraries/LibWeb/WebDriver/ElementReference.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/ElementReference.cpp @@ -334,6 +334,47 @@ bool is_element_non_typeable_form_control(Web::DOM::Element const& element) } } +// https://w3c.github.io/webdriver/#dfn-in-view +bool is_element_in_view(ReadonlySpan> paint_tree, Web::DOM::Element& element) +{ + // An element is in view if it is a member of its own pointer-interactable paint tree, given the pretense that its + // pointer events are not disabled. + if (!element.paintable() || !element.paintable()->is_visible() || !element.paintable()->visible_for_hit_testing()) + return false; + + return paint_tree.contains_slow(JS::NonnullGCPtr { element }); +} + +// https://w3c.github.io/webdriver/#dfn-in-view +bool is_element_obscured(ReadonlySpan> paint_tree, Web::DOM::Element& element) +{ + // An element is obscured if the pointer-interactable paint tree at its center point is empty, or the first element + // in this tree is not an inclusive descendant of itself. + return paint_tree.is_empty() || !paint_tree.first()->is_shadow_including_inclusive_descendant_of(element); +} + +// https://w3c.github.io/webdriver/#dfn-pointer-interactable-paint-tree +JS::MarkedVector> pointer_interactable_tree(Web::HTML::BrowsingContext& browsing_context, Web::DOM::Element& element) +{ + // 1. If element is not in the same tree as session's current browsing context's active document, return an empty sequence. + if (!browsing_context.active_document()->contains(element)) + return JS::MarkedVector>(browsing_context.heap()); + + // 2. Let rectangles be the DOMRect sequence returned by calling getClientRects(). + auto rectangles = element.get_client_rects(); + + // 3. If rectangles has the length of 0, return an empty sequence. + if (rectangles->length() == 0) + return JS::MarkedVector>(browsing_context.heap()); + + // 4. Let center point be the in-view center point of the first indexed element in rectangles. + auto viewport = browsing_context.page().top_level_traversable()->viewport_rect(); + auto center_point = Web::WebDriver::in_view_center_point(element, viewport); + + // 5. Return the elements from point given the coordinates center point. + return browsing_context.active_document()->elements_from_point(center_point.x().to_double(), center_point.y().to_double()); +} + // https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference ByteString get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root) { diff --git a/Userland/Libraries/LibWeb/WebDriver/ElementReference.h b/Userland/Libraries/LibWeb/WebDriver/ElementReference.h index 905521b64fb..09ba532cb21 100644 --- a/Userland/Libraries/LibWeb/WebDriver/ElementReference.h +++ b/Userland/Libraries/LibWeb/WebDriver/ElementReference.h @@ -38,6 +38,10 @@ bool is_element_mutable(Web::DOM::Element const&); bool is_element_mutable_form_control(Web::DOM::Element const&); bool is_element_non_typeable_form_control(Web::DOM::Element const&); +bool is_element_in_view(ReadonlySpan> paint_tree, Web::DOM::Element&); +bool is_element_obscured(ReadonlySpan> paint_tree, Web::DOM::Element&); +JS::MarkedVector> pointer_interactable_tree(Web::HTML::BrowsingContext&, Web::DOM::Element&); + ByteString get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root); JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root); ErrorOr, Web::WebDriver::Error> get_known_shadow_root(StringView shadow_id); diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 18830586a47..14e0dd83fcd 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -1347,8 +1347,15 @@ Messages::WebDriverClient::ElementClickResponse WebDriverConnection::element_cli auto element_container = container_for_element(*element); scroll_element_into_view(*element_container); - // FIXME: 6. If element’s container is still not in view, return error with error code element not interactable. - // FIXME: 7. If element’s container is obscured by another element, return error with error code element click intercepted. + auto paint_tree = Web::WebDriver::pointer_interactable_tree(current_browsing_context(), *element_container); + + // 6. If element’s container is still not in view, return error with error code element not interactable. + if (!Web::WebDriver::is_element_in_view(paint_tree, *element_container)) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ElementNotInteractable, "Could not bring element into view"sv); + + // 7. If element’s container is obscured by another element, return error with error code element click intercepted. + if (Web::WebDriver::is_element_obscured(paint_tree, *element_container)) + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ElementClickIntercepted, "Element is obscured by another element"sv); auto on_complete = JS::create_heap_function(current_browsing_context().heap(), [this](Web::WebDriver::Response result) { // 9. Wait until the user agent event loop has spun enough times to process the DOM events generated by the