/
RelativeLocator.java
268 lines (222 loc) · 9.21 KB
/
RelativeLocator.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.support.locators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import java.util.List;
import java.util.Map;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.support.locators.RelativeLocatorScript.FIND_ELEMENTS;
/**
* Used for finding elements by their location on a page, rather than their
* position on the DOM. Elements are returned ordered by their proximity to
* the last anchor element used for finding them. As an example:
* <pre>
* List<WebElement> elements = driver.findElements(with(tagName("p")).above(lowest));
* </pre>
* Would return all {@code p} elements above the {@link WebElement}
* {@code lowest} sorted by the proximity to {@code lowest}.
* <p>
* Proximity is determined by simply comparing the distance to the center
* point of each of the elements in turn. For some non-rectangular shapes
* (such as paragraphs of text that take more than one line), this may lead
* to some surprising results.
* <p>
* In addition, be aware that the relative locators all use the "client
* bounding rect" of elements to determine whether something is "left",
* "right", "above" or "below" of another. Given the example:
* <pre>
* +-----+
* | a |---+
* +-----+ b |
* +-----+
* </pre>
* Where {@code a} partially overlaps {@code b}, {@code b} is none of
* "above", "below", "left" or "right" of {@code a}. This is because of
* how these are calculated using the box model. {@code b}'s bounding
* rect has it's left-most edge to the right of {@code a}'s bounding
* rect's right-most edge, so it is not considered to be "right" of
* {@code a}. Similar logic applies for the other directions.
*/
public class RelativeLocator {
private static final Json JSON = new Json();
private static final int CLOSE_IN_PIXELS = 100;
/**
* Start of a relative locator, finding elements by tag name.
*/
public static RelativeBy with(By by) {
Require.nonNull("By to look for", by);
return new RelativeBy(by);
}
public static class RelativeBy extends By implements By.Remotable {
private final Object root;
private final List<Map<String, Object>> filters;
private RelativeBy(Object rootLocator) {
this(rootLocator, ImmutableList.of());
}
private RelativeBy(Object rootLocator, List<Map<String, Object>> filters) {
if (rootLocator instanceof By) {
assertLocatorCanBeSerialized(rootLocator);
rootLocator = asAtomLocatorParameter(rootLocator);
} else if (rootLocator instanceof Map) {
if (((Map<?, ?>) rootLocator).keySet().size() != 1) {
throw new IllegalArgumentException(
"Root locators as find element payload must only have a single key: " + rootLocator);
}
} else if (!(rootLocator instanceof WebElement)) {
throw new IllegalArgumentException("Root locator must be an element or a locator: " + rootLocator);
}
this.root = Require.nonNull("Root locator", rootLocator);
this.filters = ImmutableList.copyOf(Require.nonNull("Filters", filters));
}
public RelativeBy above(WebElement element) {
Require.nonNull("Element to search above of", element);
return simpleDirection("above", element);
}
public RelativeBy above(By locator) {
Require.nonNull("Locator", locator);
assertLocatorCanBeSerialized(locator);
return simpleDirection("above", locator);
}
public RelativeBy below(WebElement element) {
Require.nonNull("Element to search below of", element);
return simpleDirection("below", element);
}
public RelativeBy below(By locator) {
Require.nonNull("Locator", locator);
assertLocatorCanBeSerialized(locator);
return simpleDirection("below", locator);
}
public RelativeBy toLeftOf(WebElement element) {
Require.nonNull("Element to search to left of", element);
return simpleDirection("left", element);
}
public RelativeBy toLeftOf(By locator) {
Require.nonNull("Locator", locator);
assertLocatorCanBeSerialized(locator);
return simpleDirection("left", locator);
}
public RelativeBy toRightOf(WebElement element) {
Require.nonNull("Element to search to right of", element);
return simpleDirection("right", element);
}
public RelativeBy toRightOf(By locator) {
Require.nonNull("Locator", locator);
assertLocatorCanBeSerialized(locator);
return simpleDirection("right", locator);
}
public RelativeBy near(WebElement element) {
Require.nonNull("Element to search near", element);
return near(element, CLOSE_IN_PIXELS);
}
public RelativeBy near(WebElement element, int atMostDistanceInPixels) {
Require.nonNull("Element to search near", element);
Require.positive("Distance", atMostDistanceInPixels);
return near((Object) element, atMostDistanceInPixels);
}
public RelativeBy near(By locator) {
Require.nonNull("Locator", locator);
return near((Object) locator, CLOSE_IN_PIXELS);
}
public RelativeBy near(By locator, int atMostDistanceInPixels) {
Require.nonNull("Locator", locator);
Require.positive("Distance", atMostDistanceInPixels);
return near((Object) locator, atMostDistanceInPixels);
}
private RelativeBy near(Object locator, int atMostDistanceInPixels) {
Require.nonNull("Locator", locator);
Require.positive("Distance", atMostDistanceInPixels);
return new RelativeBy(
root,
amend(ImmutableMap.of(
"kind", "near",
"args", ImmutableList.of(asAtomLocatorParameter(locator), atMostDistanceInPixels))));
}
@Override
public List<WebElement> findElements(SearchContext context) {
JavascriptExecutor js = getJavascriptExecutor(context);
@SuppressWarnings("unchecked")
List<WebElement> elements = (List<WebElement>) js.executeScript(FIND_ELEMENTS, asAtomLocatorParameter(this));
return elements;
}
private RelativeBy simpleDirection(String direction, Object locator) {
Require.nonNull("Direction to search in", direction);
Require.nonNull("Locator", locator);
return new RelativeBy(
root,
amend(ImmutableMap.of(
"kind", direction,
"args", ImmutableList.of(asAtomLocatorParameter(locator)))));
}
private List<Map<String, Object>> amend(Map<String, Object> toAdd) {
return ImmutableList.<Map<String, Object>>builder()
.addAll(filters)
.add(toAdd)
.build();
}
@Override
public Parameters getRemoteParameters() {
return new Parameters(
"relative",
ImmutableMap.of("root", root, "filters", filters));
}
private Map<String, Object> toJson() {
return ImmutableMap.of(
"using", "relative",
"value", ImmutableMap.of("root", root, "filters", filters));
}
}
private static Object asAtomLocatorParameter(Object object) {
if (object instanceof WebElement) {
return object;
}
if (!(object instanceof By)) {
throw new IllegalArgumentException("Expected locator to be either an element or a By: " + object);
}
assertLocatorCanBeSerialized(object);
Map<String, Object> raw = JSON.toType(JSON.toJson(object), MAP_TYPE);
if (!(raw.get("using") instanceof String)) {
throw new JsonException("Expected JSON encoded form of locator to have a 'using' field. " + raw);
}
if (!raw.containsKey("value")) {
throw new JsonException("Expected JSON encoded form of locator to have a 'value' field: " + raw);
}
return ImmutableMap.of((String) raw.get("using"), raw.get("value"));
}
private static void assertLocatorCanBeSerialized(Object locator) {
Require.nonNull("Locator", locator);
Class<?> clazz = locator.getClass();
while (!clazz.equals(Object.class)) {
try {
clazz.getDeclaredMethod("toJson");
return;
} catch (NoSuchMethodException e) {
// Do nothing. Continue with the loop
}
clazz = clazz.getSuperclass();
}
throw new IllegalArgumentException(
"Locator must be serializable to JSON using a `toJson` method. " + locator);
}
}