18
18
package org .openqa .selenium .remote ;
19
19
20
20
import com .google .common .collect .ImmutableMap ;
21
+
21
22
import org .junit .jupiter .api .Test ;
22
23
import org .junit .jupiter .api .Tag ;
23
24
import org .openqa .selenium .By ;
50
51
import static org .openqa .selenium .remote .DriverCommand .FIND_ELEMENT ;
51
52
52
53
@ Tag ("UnitTests" )
53
- public class AugmenterTest {
54
+ class AugmenterTest {
54
55
55
56
private Augmenter getAugmenter () {
56
57
return new Augmenter ();
57
58
}
58
59
59
60
@ Test
60
- public void shouldAugmentRotatable () {
61
+ void shouldAugmentRotatable () {
61
62
final Capabilities caps = new ImmutableCapabilities (CapabilityType .ROTATABLE , true );
62
63
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
63
64
@@ -68,8 +69,10 @@ public void shouldAugmentRotatable() {
68
69
}
69
70
70
71
@ Test
71
- public void shouldAugmentLocationContext () {
72
- final Capabilities caps = new ImmutableCapabilities (CapabilityType .SUPPORTS_LOCATION_CONTEXT , true );
72
+ void shouldAugmentLocationContext () {
73
+ final Capabilities
74
+ caps =
75
+ new ImmutableCapabilities (CapabilityType .SUPPORTS_LOCATION_CONTEXT , true );
73
76
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
74
77
75
78
WebDriver returned = getAugmenter ().augment (driver );
@@ -79,7 +82,7 @@ public void shouldAugmentLocationContext() {
79
82
}
80
83
81
84
@ Test
82
- public void shouldAddInterfaceFromCapabilityIfNecessary () {
85
+ void shouldAddInterfaceFromCapabilityIfNecessary () {
83
86
final Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true );
84
87
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
85
88
@@ -92,7 +95,7 @@ public void shouldAddInterfaceFromCapabilityIfNecessary() {
92
95
}
93
96
94
97
@ Test
95
- public void shouldNotAddInterfaceWhenBooleanValueForItIsFalse () {
98
+ void shouldNotAddInterfaceWhenBooleanValueForItIsFalse () {
96
99
Capabilities caps = new ImmutableCapabilities ("magic.numbers" , false );
97
100
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
98
101
@@ -105,7 +108,7 @@ public void shouldNotAddInterfaceWhenBooleanValueForItIsFalse() {
105
108
}
106
109
107
110
@ Test
108
- public void shouldNotUseNonMatchingInterfaces () {
111
+ void shouldNotUseNonMatchingInterfaces () {
109
112
Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true );
110
113
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
111
114
@@ -116,7 +119,7 @@ public void shouldNotUseNonMatchingInterfaces() {
116
119
}
117
120
118
121
@ Test
119
- public void shouldDelegateToHandlerIfAdded () {
122
+ void shouldDelegateToHandlerIfAdded () {
120
123
Capabilities caps = new ImmutableCapabilities ("foo" , true );
121
124
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
122
125
@@ -132,7 +135,7 @@ public void shouldDelegateToHandlerIfAdded() {
132
135
}
133
136
134
137
@ Test
135
- public void shouldDelegateUnmatchedMethodCallsToDriverImplementation () {
138
+ void shouldDelegateUnmatchedMethodCallsToDriverImplementation () {
136
139
Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true );
137
140
StubExecutor stubExecutor = new StubExecutor (caps );
138
141
stubExecutor .expect (DriverCommand .GET_TITLE , new HashMap <>(), "Title" );
@@ -149,7 +152,7 @@ public void shouldDelegateUnmatchedMethodCallsToDriverImplementation() {
149
152
}
150
153
151
154
@ Test
152
- public void proxyShouldNotAppearInStackTraces () {
155
+ void proxyShouldNotAppearInStackTraces () {
153
156
// This will force the class to be enhanced
154
157
final Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true );
155
158
@@ -168,21 +171,21 @@ public void proxyShouldNotAppearInStackTraces() {
168
171
}
169
172
170
173
@ Test
171
- public void shouldCopyFieldsFromTemplateInstanceIntoChildInstance () {
174
+ void shouldCopyFieldsFromTemplateInstanceIntoChildInstance () {
172
175
ChildRemoteDriver driver = new ChildRemoteDriver ();
173
176
HasMagicNumbers holder = (HasMagicNumbers ) getAugmenter ().augment (driver );
174
177
175
178
assertThat (holder .getMagicNumber ()).isEqualTo (3 );
176
179
}
177
180
178
181
@ Test
179
- public void shouldNotChokeOnFinalFields () {
182
+ void shouldNotChokeOnFinalFields () {
180
183
WithFinals withFinals = new WithFinals ();
181
184
getAugmenter ().augment (withFinals );
182
185
}
183
186
184
187
@ Test
185
- public void shouldAllowReflexiveCalls () {
188
+ void shouldAllowReflexiveCalls () {
186
189
Capabilities caps = new ImmutableCapabilities ("find by magic" , true );
187
190
StubExecutor executor = new StubExecutor (caps );
188
191
final WebElement element = mock (WebElement .class );
@@ -205,30 +208,33 @@ public void shouldAllowReflexiveCalls() {
205
208
}
206
209
207
210
@ Test
208
- public void shouldAugmentMultipleInterfaces () {
211
+ void shouldAugmentMultipleInterfaces () {
209
212
final Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true ,
210
213
"numbers" , true );
211
214
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
212
215
213
216
WebDriver returned = getAugmenter ()
214
217
.addDriverAugmentation ("magic.numbers" , HasMagicNumbers .class , (c , exe ) -> () -> 42 )
215
- .addDriverAugmentation ("numbers" , HasNumbers .class , (c , exe ) -> webDriver -> {
216
- Require .precondition (webDriver instanceof HasMagicNumbers , "Driver must implement HasMagicNumbers" );
217
- return ((HasMagicNumbers )webDriver ).getMagicNumber ();
218
+ .addDriverAugmentation ("numbers" , HasNumbers .class , (c , exe ) -> webDriver -> {
219
+ Require .precondition (webDriver instanceof HasMagicNumbers ,
220
+ "Driver must implement HasMagicNumbers" );
221
+ return ((HasMagicNumbers ) webDriver ).getMagicNumber ();
218
222
})
219
223
.augment (driver );
220
224
221
225
assertThat (returned ).isNotSameAs (driver );
222
226
assertThat (returned ).isInstanceOf (HasMagicNumbers .class );
223
227
assertThat (returned ).isInstanceOf (HasNumbers .class );
224
228
225
- int number = ((HasNumbers )returned ).getNumbers (returned );
229
+ int number = ((HasNumbers ) returned ).getNumbers (returned );
226
230
assertThat (number ).isEqualTo (42 );
227
231
}
228
232
229
233
@ Test
230
- public void shouldAugmentWebDriverDecorator () {
231
- final Capabilities caps = new ImmutableCapabilities (CapabilityType .SUPPORTS_LOCATION_CONTEXT , true );
234
+ void shouldAugmentWebDriverDecorator () {
235
+ final Capabilities
236
+ caps =
237
+ new ImmutableCapabilities (CapabilityType .SUPPORTS_LOCATION_CONTEXT , true );
232
238
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
233
239
234
240
WebDriver decorated = new ModifyTitleWebDriverDecorator ().decorate (driver );
@@ -241,22 +247,23 @@ public void shouldAugmentWebDriverDecorator() {
241
247
assertThat (returned ).isNotSameAs (decorated );
242
248
assertThat (returned ).isInstanceOf (LocationContext .class );
243
249
244
- String title = returned .getTitle ();
250
+ String title = returned .getTitle ();
245
251
246
252
assertThat (title ).isEqualTo ("title" );
247
253
}
248
254
249
255
@ Test
250
- public void shouldDecorateAugmentedWebDriver () {
256
+ void shouldDecorateAugmentedWebDriver () {
251
257
final Capabilities caps = new ImmutableCapabilities ("magic.numbers" , true ,
252
258
"numbers" , true );
253
259
WebDriver driver = new RemoteWebDriver (new StubExecutor (caps ), caps );
254
260
255
261
WebDriver augmented = getAugmenter ()
256
262
.addDriverAugmentation ("magic.numbers" , HasMagicNumbers .class , (c , exe ) -> () -> 42 )
257
- .addDriverAugmentation ("numbers" , HasNumbers .class , (c , exe ) -> webDriver -> {
258
- Require .precondition (webDriver instanceof HasMagicNumbers , "Driver must implement HasMagicNumbers" );
259
- return ((HasMagicNumbers )webDriver ).getMagicNumber ();
263
+ .addDriverAugmentation ("numbers" , HasNumbers .class , (c , exe ) -> webDriver -> {
264
+ Require .precondition (webDriver instanceof HasMagicNumbers ,
265
+ "Driver must implement HasMagicNumbers" );
266
+ return ((HasMagicNumbers ) webDriver ).getMagicNumber ();
260
267
})
261
268
.augment (driver );
262
269
@@ -271,11 +278,12 @@ public void shouldDecorateAugmentedWebDriver() {
271
278
272
279
assertThat (title ).isEqualTo ("title" );
273
280
274
- int number = ((HasNumbers )decorated ).getNumbers (decorated );
281
+ int number = ((HasNumbers ) decorated ).getNumbers (decorated );
275
282
assertThat (number ).isEqualTo (42 );
276
283
}
277
284
278
285
private static class ByMagic extends By {
286
+
279
287
private final String magicWord ;
280
288
281
289
public ByMagic (String magicWord ) {
@@ -289,17 +297,18 @@ public List<WebElement> findElements(SearchContext context) {
289
297
}
290
298
291
299
public interface FindByMagic {
300
+
292
301
WebElement findByMagic (String magicWord );
293
302
}
294
303
295
304
@ Test
296
- public void shouldBeAbleToAugmentMultipleTimes () {
305
+ void shouldBeAbleToAugmentMultipleTimes () {
297
306
Capabilities caps = new ImmutableCapabilities ("rotatable" , true , "magic.numbers" , true );
298
307
299
308
StubExecutor stubExecutor = new StubExecutor (caps );
300
309
stubExecutor .expect (DriverCommand .GET_SCREEN_ORIENTATION ,
301
- Collections .emptyMap (),
302
- ScreenOrientation .PORTRAIT .name ());
310
+ Collections .emptyMap (),
311
+ ScreenOrientation .PORTRAIT .name ());
303
312
RemoteWebDriver driver = new RemoteWebDriver (stubExecutor , caps );
304
313
305
314
WebDriver augmented = getAugmenter ().augment (driver );
@@ -326,6 +335,7 @@ public void shouldBeAbleToAugmentMultipleTimes() {
326
335
}
327
336
328
337
protected static class StubExecutor implements CommandExecutor {
338
+
329
339
private final Capabilities capabilities ;
330
340
private final List <Data > expected = new ArrayList <>();
331
341
@@ -343,7 +353,7 @@ public Response execute(Command command) {
343
353
344
354
for (Data possibleMatch : expected ) {
345
355
if (possibleMatch .commandName .equals (command .getName ()) &&
346
- possibleMatch .args .equals (command .getParameters ())) {
356
+ possibleMatch .args .equals (command .getParameters ())) {
347
357
Response response = new Response (new SessionId ("foo" ));
348
358
response .setValue (possibleMatch .returnValue );
349
359
return response ;
@@ -358,6 +368,7 @@ public void expect(String commandName, Map<String, ?> args, Object returnValue)
358
368
}
359
369
360
370
private static class Data {
371
+
361
372
public String commandName ;
362
373
public Map <String , ?> args ;
363
374
public Object returnValue ;
@@ -371,10 +382,12 @@ public Data(String commandName, Map<String, ?> args, Object returnValue) {
371
382
}
372
383
373
384
public interface MyInterface {
385
+
374
386
String getHelloWorld ();
375
387
}
376
388
377
389
public static class DetonatingDriver extends RemoteWebDriver {
390
+
378
391
private Capabilities caps ;
379
392
380
393
public void setCapabilities (Capabilities caps ) {
@@ -398,10 +411,12 @@ public WebElement findElement(By locator) {
398
411
}
399
412
400
413
public interface HasNumbers {
414
+
401
415
int getNumbers (WebDriver driver );
402
416
}
403
417
404
418
public static class ChildRemoteDriver extends RemoteWebDriver implements HasMagicNumbers {
419
+
405
420
private int magicNumber = 3 ;
406
421
407
422
@ Override
@@ -416,6 +431,7 @@ public int getMagicNumber() {
416
431
}
417
432
418
433
public static class WithFinals extends RemoteWebDriver {
434
+
419
435
public final String finalField = "FINAL" ;
420
436
421
437
@ Override
0 commit comments