-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
RemoteWebElementTest.java
385 lines (313 loc) · 14.4 KB
/
RemoteWebElementTest.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
// 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 static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.openqa.selenium.remote.WebDriverFixture.echoCapabilities;
import static org.openqa.selenium.remote.WebDriverFixture.errorResponder;
import static org.openqa.selenium.remote.WebDriverFixture.exceptionResponder;
import static org.openqa.selenium.remote.WebDriverFixture.nullValueResponder;
import static org.openqa.selenium.remote.WebDriverFixture.valueResponder;
import static org.openqa.selenium.remote.WebDriverFixture.webDriverExceptionResponder;
import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.internal.Debug;
@Tag("UnitTests")
class RemoteWebElementTest {
@Test
void throwIfRemoteEndReturnsNullFromFindChild() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, nullValueResponder);
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> fixture.element.findElement(By.id("id")));
}
@Test
void canHandleClickCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, nullValueResponder);
fixture.element.click();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleWebDriverExceptionThrownByCommandExecutor() {
WebElementFixture fixture =
new WebElementFixture(
new ImmutableCapabilities("browserName", "cheese", "platformName", "WINDOWS"),
echoCapabilities,
webDriverExceptionResponder);
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(fixture.element::click)
.withMessageStartingWith("BOOM!!!")
.withMessageContaining("Build info: ")
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
.withMessageContaining(String.format("Session ID: %s", fixture.driver.getSessionId()))
.withMessageContaining(String.format("%s", fixture.driver.getCapabilities()))
.withMessageContaining(
String.format(
"Command: [%s, clickElement {id=%s}]",
fixture.driver.getSessionId(), fixture.element.getId()))
.withMessageContaining(
String.format(
"Element: [[RemoteWebDriver: cheese on windows (%s)] -> id: test]",
fixture.driver.getSessionId()));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGeneralExceptionThrownByCommandExecutor() {
try (MockedStatic<Debug> debugMock = Mockito.mockStatic(Debug.class)) {
debugMock.when(Debug::isDebugging).thenReturn(true);
WebElementFixture fixture = new WebElementFixture(
new ImmutableCapabilities("browserName", "cheese", "platformName", "WINDOWS"),
echoCapabilities, exceptionResponder);
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(fixture.element::click)
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
.withMessageContaining("Build info: ")
.withMessageContaining(
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
.withMessageContaining(String.format(
"Session ID: %s", fixture.driver.getSessionId()))
.withMessageContaining(String.format(
"%s", fixture.driver.getCapabilities()))
.withMessageContaining(String.format(
"Command: [%s, clickElement {id=%s}]", fixture.driver.getSessionId(), fixture.element.getId()))
.withMessageContaining(String.format(
"Element: [[RemoteWebDriver: cheese on windows (%s)] -> id: test]", fixture.driver.getSessionId()))
.havingCause()
.withMessage("BOOM!!!");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
}
@Test
void canHandleWebDriverExceptionReturnedByCommandExecutor() {
WebElementFixture fixture =
new WebElementFixture(
new ImmutableCapabilities("browserName", "cheese"),
echoCapabilities,
errorResponder("element click intercepted", new WebDriverException("BOOM!!!")));
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(fixture.element::click)
.withMessageStartingWith("BOOM!!!")
.withMessageContaining("Build info: ")
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
.withMessageContaining(String.format("Session ID: %s", fixture.driver.getSessionId()))
.withMessageContaining(String.format("%s", fixture.driver.getCapabilities()))
.withMessageContaining(
String.format(
"Command: [%s, clickElement {id=%s}]",
fixture.driver.getSessionId(), fixture.element.getId()))
.withMessageContaining(
String.format(
"Element: [[RemoteWebDriver: cheese on any (%s)] -> id: test]",
fixture.driver.getSessionId()));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleResponseWithErrorCodeButNoExceptionReturnedByCommandExecutor() {
WebElementFixture fixture =
new WebElementFixture(
new ImmutableCapabilities("browserName", "cheese"),
echoCapabilities,
errorResponder("element click intercepted", "BOOM!!!"));
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(fixture.element::click)
.withMessageStartingWith("BOOM!!!")
.withMessageContaining("Build info: ")
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
.withMessageContaining(String.format("Session ID: %s", fixture.driver.getSessionId()))
.withMessageContaining(String.format("%s", fixture.driver.getCapabilities()))
.withMessageContaining(
String.format(
"Command: [%s, clickElement {id=%s}]",
fixture.driver.getSessionId(), fixture.element.getId()))
.withMessageContaining(
String.format(
"Element: [[RemoteWebDriver: cheese on any (%s)] -> id: test]",
fixture.driver.getSessionId()));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleClearCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, nullValueResponder);
fixture.element.clear();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.CLEAR_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleSubmitCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, nullValueResponder);
fixture.element.submit();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.SUBMIT_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleSendKeysCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, nullValueResponder);
fixture.element.setFileDetector(mock(FileDetector.class));
fixture.element.sendKeys("test");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.SEND_KEYS_TO_ELEMENT,
ImmutableMap.of("id", fixture.element.getId(), "value", new CharSequence[] {"test"})));
}
@Test
void canHandleGetAttributeCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("test"));
assertThat(fixture.element.getAttribute("id")).isEqualTo("test");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_ATTRIBUTE,
ImmutableMap.of("id", fixture.element.getId(), "name", "id")));
}
@Test
void canHandleGetDomAttributeCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("test"));
assertThat(fixture.element.getDomAttribute("id")).isEqualTo("test");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_DOM_ATTRIBUTE,
ImmutableMap.of("id", fixture.element.getId(), "name", "id")));
}
@Test
void canHandleGetDomPropertyCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("test"));
assertThat(fixture.element.getDomProperty("id")).isEqualTo("test");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_DOM_PROPERTY,
ImmutableMap.of("id", fixture.element.getId(), "name", "id")));
}
@Test
void canHandleIsSelectedCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder(true));
assertThat(fixture.element.isSelected()).isTrue();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.IS_ELEMENT_SELECTED, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleIsEnabledCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder(true));
assertThat(fixture.element.isEnabled()).isTrue();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.IS_ELEMENT_ENABLED, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleIsDisplayedCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder(true));
assertThat(fixture.element.isDisplayed()).isTrue();
fixture.verifyCommands(
new CommandPayload(
DriverCommand.IS_ELEMENT_DISPLAYED, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGeTextCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("test"));
assertThat(fixture.element.getText()).isEqualTo("test");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_TEXT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetTagNameCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("div"));
assertThat(fixture.element.getTagName()).isEqualTo("div");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_TAG_NAME, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetLocationCommand() {
WebElementFixture fixture =
new WebElementFixture(echoCapabilities, valueResponder(ImmutableMap.of("x", 10, "y", 20)));
assertThat(fixture.element.getLocation()).isEqualTo(new Point(10, 20));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_RECT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetSizeCommand() {
WebElementFixture fixture =
new WebElementFixture(
echoCapabilities, valueResponder(ImmutableMap.of("width", 100, "height", 200)));
assertThat(fixture.element.getSize()).isEqualTo(new Dimension(100, 200));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_RECT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetRectCommand() {
WebElementFixture fixture =
new WebElementFixture(
echoCapabilities,
valueResponder(ImmutableMap.of("x", 10, "y", 20, "width", 100, "height", 200)));
assertThat(fixture.element.getRect())
.isEqualTo(new Rectangle(new Point(10, 20), new Dimension(100, 200)));
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_RECT, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetCssPropertyCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("red"));
assertThat(fixture.element.getCssValue("color")).isEqualTo("red");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_VALUE_OF_CSS_PROPERTY,
ImmutableMap.of("id", fixture.element.getId(), "propertyName", "color")));
}
@Test
void canHandleGetAriaRoleCommand() {
WebElementFixture fixture = new WebElementFixture(echoCapabilities, valueResponder("section"));
assertThat(fixture.element.getAriaRole()).isEqualTo("section");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_ARIA_ROLE, ImmutableMap.of("id", fixture.element.getId())));
}
@Test
void canHandleGetAccessibleNameCommand() {
WebElementFixture fixture =
new WebElementFixture(echoCapabilities, valueResponder("element name"));
assertThat(fixture.element.getAccessibleName()).isEqualTo("element name");
fixture.verifyCommands(
new CommandPayload(
DriverCommand.GET_ELEMENT_ACCESSIBLE_NAME,
ImmutableMap.of("id", fixture.element.getId())));
}
}