/* * Copyright (c) 2022, Tobias Christiansen * Copyright (c) 2022, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include namespace Web::WebDriver { // https://w3c.github.io/webdriver/#css-selectors static ErrorOr, Error> locate_element_by_css_selector(DOM::ParentNode& start_node, StringView selector) { // 1. Let elements be the result of calling querySelectorAll() with start node as this and selector as the argument. // If this causes an exception to be thrown, return error with error code invalid selector. auto elements = start_node.query_selector_all(selector); if (elements.is_exception()) return Error::from_code(ErrorCode::InvalidSelector, "querySelectorAll() failed"sv); // 2.Return success with data elements. return elements.release_value(); } // https://w3c.github.io/webdriver/#link-text static ErrorOr, Error> locate_element_by_link_text(DOM::ParentNode&, StringView) { return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by link text"sv); } // https://w3c.github.io/webdriver/#partial-link-text static ErrorOr, Error> locate_element_by_partial_link_text(DOM::ParentNode&, StringView) { return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by partial link text"sv); } // https://w3c.github.io/webdriver/#tag-name static ErrorOr, Error> locate_element_by_tag_name(DOM::ParentNode&, StringView) { return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by tag name"sv); } // https://w3c.github.io/webdriver/#xpath static ErrorOr, Error> locate_element_by_x_path(DOM::ParentNode&, StringView) { return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by XPath"sv); } Optional location_strategy_from_string(StringView type) { if (type == "css selector"sv) return LocationStrategy::CssSelector; if (type == "link text"sv) return LocationStrategy::LinkText; if (type == "partial link text"sv) return LocationStrategy::PartialLinkText; if (type == "tag name"sv) return LocationStrategy::TagName; if (type == "xpath"sv) return LocationStrategy::XPath; return {}; } ErrorOr, Error> invoke_location_strategy(LocationStrategy type, DOM::ParentNode& start_node, StringView selector) { switch (type) { case LocationStrategy::CssSelector: return locate_element_by_css_selector(start_node, selector); case LocationStrategy::LinkText: return locate_element_by_link_text(start_node, selector); case LocationStrategy::PartialLinkText: return locate_element_by_partial_link_text(start_node, selector); case LocationStrategy::TagName: return locate_element_by_tag_name(start_node, selector); case LocationStrategy::XPath: return locate_element_by_x_path(start_node, selector); } VERIFY_NOT_REACHED(); } }