-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
RemoteWebElement.java
418 lines (360 loc) · 12.1 KB
/
RemoteWebElement.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 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.remote;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Beta;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.WrapsElement;
import org.openqa.selenium.interactions.Coordinates;
import org.openqa.selenium.interactions.Locatable;
import org.openqa.selenium.io.Zip;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.openqa.selenium.remote.DriverCommand.FIND_CHILD_ELEMENT;
import static org.openqa.selenium.remote.DriverCommand.FIND_CHILD_ELEMENTS;
public class RemoteWebElement implements WebElement, Locatable, TakesScreenshot, WrapsDriver {
private String foundBy;
protected String id;
protected RemoteWebDriver parent;
protected FileDetector fileDetector;
protected void setFoundBy(SearchContext foundFrom, String locator, String term) {
this.foundBy = String.format("[%s] -> %s: %s", foundFrom, locator, term);
}
public void setParent(RemoteWebDriver parent) {
this.parent = parent;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setFileDetector(FileDetector detector) {
fileDetector = detector;
}
@Override
public void click() {
execute(DriverCommand.CLICK_ELEMENT(id));
}
@Override
public void submit() {
try {
execute(DriverCommand.SUBMIT_ELEMENT(id));
} catch (JavascriptException ex) {
String message = "To submit an element, it must be nested inside a form element";
throw new UnsupportedOperationException(message);
}
}
@Override
public void sendKeys(CharSequence... keysToSend) {
if (keysToSend == null || keysToSend.length == 0) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
for (CharSequence cs : keysToSend) {
if (cs == null) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
}
String allKeysToSend = String.join("", keysToSend);
List<File> files = Arrays.stream(allKeysToSend.split("\n"))
.map(fileDetector::getLocalFile)
.collect(Collectors.toList());
if (!files.isEmpty() && !files.contains(null)) {
allKeysToSend = files.stream()
.map(this::upload)
.collect(Collectors.joining("\n"));
}
execute(DriverCommand.SEND_KEYS_TO_ELEMENT(id, new CharSequence[]{allKeysToSend}));
}
private String upload(File localFile) {
if (!localFile.isFile()) {
throw new WebDriverException("You may only upload files: " + localFile);
}
try {
String zip = Zip.zip(localFile);
Response response = execute(DriverCommand.UPLOAD_FILE(zip));
return (String) response.getValue();
} catch (IOException e) {
throw new WebDriverException("Cannot upload " + localFile, e);
}
}
@Override
public void clear() {
execute(DriverCommand.CLEAR_ELEMENT(id));
}
@Override
public String getTagName() {
return (String) execute(DriverCommand.GET_ELEMENT_TAG_NAME(id))
.getValue();
}
@Override
public String getDomProperty(String name) {
return stringValueOf(
execute(DriverCommand.GET_ELEMENT_DOM_PROPERTY(id, name))
.getValue());
}
@Override
public String getDomAttribute(String name) {
return stringValueOf(
execute(DriverCommand.GET_ELEMENT_DOM_ATTRIBUTE(id, name))
.getValue());
}
@Override
public String getAttribute(String name) {
return stringValueOf(
execute(DriverCommand.GET_ELEMENT_ATTRIBUTE(id, name))
.getValue());
}
@Override
public String getAriaRole() {
return stringValueOf(
execute(DriverCommand.GET_ELEMENT_ARIA_ROLE(id))
.getValue());
}
@Override
public String getAccessibleName() {
return stringValueOf(
execute(DriverCommand.GET_ELEMENT_ACCESSIBLE_NAME(id))
.getValue());
}
private static String stringValueOf(Object o) {
if (o == null) {
return null;
}
return String.valueOf(o);
}
@Override
public boolean isSelected() {
Object value = execute(DriverCommand.IS_ELEMENT_SELECTED(id))
.getValue();
try {
return (Boolean) value;
} catch (ClassCastException ex) {
throw new WebDriverException("Returned value cannot be converted to Boolean: " + value, ex);
}
}
@Override
public boolean isEnabled() {
Object value = execute(DriverCommand.IS_ELEMENT_ENABLED(id))
.getValue();
try {
return (Boolean) value;
} catch (ClassCastException ex) {
throw new WebDriverException("Returned value cannot be converted to Boolean: " + value, ex);
}
}
@Override
public String getText() {
Response response = execute(DriverCommand.GET_ELEMENT_TEXT(id));
return (String) response.getValue();
}
@Override
public String getCssValue(String propertyName) {
Response response = execute(DriverCommand.GET_ELEMENT_VALUE_OF_CSS_PROPERTY(id, propertyName));
return (String) response.getValue();
}
@Override
public List<WebElement> findElements(By locator) {
return parent.findElements(
this,
(using, value) -> FIND_CHILD_ELEMENTS(getId(), using, String.valueOf(value)),
locator);
}
@Override
public WebElement findElement(By locator) {
return parent.findElement(
this,
(using, value) -> FIND_CHILD_ELEMENT(getId(), using, String.valueOf(value)),
locator);
}
/**
* @deprecated Rely on using {@link By.Remotable} instead
*/
@Deprecated
protected WebElement findElement(String using, String value) {
throw new UnsupportedOperationException("`findElement` has been replaced by usages of " + By.Remotable.class);
}
/**
* @deprecated Rely on using {@link By.Remotable} instead
*/
@Deprecated
protected List<WebElement> findElements(String using, String value) {
throw new UnsupportedOperationException("`findElement` has been replaced by usages of " + By.Remotable.class);
}
@Override
public SearchContext getShadowRoot() {
Response response = execute(DriverCommand.GET_ELEMENT_SHADOW_ROOT(getId()));
return (SearchContext) response.getValue();
}
protected Response execute(CommandPayload payload) {
try {
return parent.execute(payload);
} catch (WebDriverException ex) {
ex.addInfo("Element", this.toString());
throw ex;
}
}
protected Response execute(String command, Map<String, ?> parameters) {
try {
return parent.execute(command, parameters);
} catch (WebDriverException ex) {
ex.addInfo("Element", this.toString());
throw ex;
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WebElement)) {
return false;
}
WebElement other = (WebElement) obj;
while (other instanceof WrapsElement) {
other = ((WrapsElement) other).getWrappedElement();
}
if (!(other instanceof RemoteWebElement)) {
return false;
}
RemoteWebElement otherRemoteWebElement = (RemoteWebElement) other;
return id.equals(otherRemoteWebElement.id);
}
/**
* @return This element's hash code, which is a hash of its internal opaque ID.
*/
@Override
public int hashCode() {
return id.hashCode();
}
/*
* (non-Javadoc)
*
* @see org.openqa.selenium.internal.WrapsDriver#getWrappedDriver()
*/
@Override
public WebDriver getWrappedDriver() {
return parent;
}
@Override
public boolean isDisplayed() {
Object value = execute(DriverCommand.IS_ELEMENT_DISPLAYED(id))
.getValue();
try {
// See https://github.com/SeleniumHQ/selenium/issues/9266
if (value == null) {
return false;
}
return (Boolean) value;
} catch (ClassCastException ex) {
throw new WebDriverException("Returned value cannot be converted to Boolean: " + value, ex);
}
}
@Override
@SuppressWarnings({"unchecked"})
public Point getLocation() {
Response response = execute(DriverCommand.GET_ELEMENT_RECT(id));
Map<String, Object> rawPoint = (Map<String, Object>) response.getValue();
int x = ((Number) rawPoint.get("x")).intValue();
int y = ((Number) rawPoint.get("y")).intValue();
return new Point(x, y);
}
@Override
@SuppressWarnings({"unchecked"})
public Dimension getSize() {
Response response = execute(DriverCommand.GET_ELEMENT_RECT(id));
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();
int width = ((Number) rawSize.get("width")).intValue();
int height = ((Number) rawSize.get("height")).intValue();
return new Dimension(width, height);
}
@Override
@SuppressWarnings({"unchecked"})
public Rectangle getRect() {
Response response = execute(DriverCommand.GET_ELEMENT_RECT(id));
Map<String, Object> rawRect = (Map<String, Object>) response.getValue();
int x = ((Number) rawRect.get("x")).intValue();
int y = ((Number) rawRect.get("y")).intValue();
int width = ((Number) rawRect.get("width")).intValue();
int height = ((Number) rawRect.get("height")).intValue();
return new Rectangle(x, y, height, width);
}
@Override
public Coordinates getCoordinates() {
return new Coordinates() {
@Override
public Point onScreen() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Point inViewPort() {
Response response = execute(DriverCommand.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW(getId()));
@SuppressWarnings("unchecked")
Map<String, Number> mapped = (Map<String, Number>) response.getValue();
return new Point(mapped.get("x").intValue(), mapped.get("y").intValue());
}
@Override
public Point onPage() {
return getLocation();
}
@Override
public Object getAuxiliary() {
return getId();
}
};
}
@Override
@Beta
public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException {
Response response = execute(DriverCommand.ELEMENT_SCREENSHOT(id));
Object result = response.getValue();
if (result instanceof String) {
String base64EncodedPng = (String) result;
return outputType.convertFromBase64Png(base64EncodedPng);
} else if (result instanceof byte[]) {
return outputType.convertFromPngBytes((byte[]) result);
} else {
throw new RuntimeException(String.format(
"Unexpected result for %s command: %s",
DriverCommand.ELEMENT_SCREENSHOT,
result == null ? "null" : result.getClass().getName() + " instance"));
}
}
public String toString() {
if (foundBy == null) {
return String.format("[%s -> unknown locator]", super.toString());
}
return String.format("[%s]", foundBy);
}
public Map<String, Object> toJson() {
return ImmutableMap.of(
Dialect.OSS.getEncodedElementKey(), getId(),
Dialect.W3C.getEncodedElementKey(), getId());
}
}