1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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();
}
}
|