diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-09 15:01:28 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-10 13:10:16 +0000 |
commit | 15916e5c148084059c22f993d650a7545305b1d3 (patch) | |
tree | 22e2dcca2b36641ebab7e0176e910d92e8e508ed /Userland/Libraries/LibWeb | |
parent | 40b07901ac3cdc4e8942bff62f4784370543003b (diff) | |
download | serenity-15916e5c148084059c22f993d650a7545305b1d3.zip |
LibWeb: Implement the element location strategies in Web::WebDriver
Diffstat (limited to 'Userland/Libraries/LibWeb')
3 files changed, 114 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 5ed7e06d14..681ea3fb92 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -438,6 +438,7 @@ set(SOURCES WebAssembly/WebAssemblyTableConstructor.cpp WebAssembly/WebAssemblyTableObject.cpp WebAssembly/WebAssemblyTablePrototype.cpp + WebDriver/ElementLocationStrategies.cpp WebDriver/Error.cpp WebDriver/ExecuteScript.cpp WebDriver/Response.cpp diff --git a/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp b/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp new file mode 100644 index 0000000000..03ef37eb34 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org> + * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibWeb/DOM/ParentNode.h> +#include <LibWeb/WebDriver/ElementLocationStrategies.h> + +namespace Web::WebDriver { + +// https://w3c.github.io/webdriver/#css-selectors +static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, 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<JS::NonnullGCPtr<DOM::NodeList>, 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<JS::NonnullGCPtr<DOM::NodeList>, 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<JS::NonnullGCPtr<DOM::NodeList>, 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<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_x_path(DOM::ParentNode&, StringView) +{ + return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by XPath"sv); +} + +Optional<LocationStrategy> 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<JS::NonnullGCPtr<DOM::NodeList>, 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(); +} + +} diff --git a/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.h b/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.h new file mode 100644 index 0000000000..8d82286a21 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/ElementLocationStrategies.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org> + * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Array.h> +#include <LibJS/Heap/GCPtr.h> +#include <LibWeb/DOM/NodeList.h> +#include <LibWeb/Forward.h> +#include <LibWeb/WebDriver/Error.h> + +namespace Web::WebDriver { + +// https://w3c.github.io/webdriver/#dfn-table-of-location-strategies +enum class LocationStrategy { + CssSelector, + LinkText, + PartialLinkText, + TagName, + XPath, +}; + +Optional<LocationStrategy> location_strategy_from_string(StringView type); +ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> invoke_location_strategy(LocationStrategy type, DOM::ParentNode& start_node, StringView selector); + +} |