Serenity Operating System
1/*
2 * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
3 * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibWeb/DOM/ParentNode.h>
9#include <LibWeb/WebDriver/ElementLocationStrategies.h>
10
11namespace Web::WebDriver {
12
13// https://w3c.github.io/webdriver/#css-selectors
14static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_css_selector(DOM::ParentNode& start_node, StringView selector)
15{
16 // 1. Let elements be the result of calling querySelectorAll() with start node as this and selector as the argument.
17 // If this causes an exception to be thrown, return error with error code invalid selector.
18 auto elements = start_node.query_selector_all(selector);
19 if (elements.is_exception())
20 return Error::from_code(ErrorCode::InvalidSelector, "querySelectorAll() failed"sv);
21
22 // 2.Return success with data elements.
23 return elements.release_value();
24}
25
26// https://w3c.github.io/webdriver/#link-text
27static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_link_text(DOM::ParentNode&, StringView)
28{
29 return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by link text"sv);
30}
31
32// https://w3c.github.io/webdriver/#partial-link-text
33static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_partial_link_text(DOM::ParentNode&, StringView)
34{
35 return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by partial link text"sv);
36}
37
38// https://w3c.github.io/webdriver/#tag-name
39static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_tag_name(DOM::ParentNode&, StringView)
40{
41 return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by tag name"sv);
42}
43
44// https://w3c.github.io/webdriver/#xpath
45static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_x_path(DOM::ParentNode&, StringView)
46{
47 return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by XPath"sv);
48}
49
50Optional<LocationStrategy> location_strategy_from_string(StringView type)
51{
52 if (type == "css selector"sv)
53 return LocationStrategy::CssSelector;
54 if (type == "link text"sv)
55 return LocationStrategy::LinkText;
56 if (type == "partial link text"sv)
57 return LocationStrategy::PartialLinkText;
58 if (type == "tag name"sv)
59 return LocationStrategy::TagName;
60 if (type == "xpath"sv)
61 return LocationStrategy::XPath;
62 return {};
63}
64
65ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> invoke_location_strategy(LocationStrategy type, DOM::ParentNode& start_node, StringView selector)
66{
67 switch (type) {
68 case LocationStrategy::CssSelector:
69 return locate_element_by_css_selector(start_node, selector);
70 case LocationStrategy::LinkText:
71 return locate_element_by_link_text(start_node, selector);
72 case LocationStrategy::PartialLinkText:
73 return locate_element_by_partial_link_text(start_node, selector);
74 case LocationStrategy::TagName:
75 return locate_element_by_tag_name(start_node, selector);
76 case LocationStrategy::XPath:
77 return locate_element_by_x_path(start_node, selector);
78 }
79
80 VERIFY_NOT_REACHED();
81}
82
83}