34
34
import org .openqa .selenium .json .Json ;
35
35
import org .openqa .selenium .json .JsonInput ;
36
36
37
+ /**
38
+ * This class performs the coercion from <b>JSON</b> to the encoded {@link Actions} object.
39
+ */
37
40
public class ActionsCoercer {
41
+
42
+ /**
43
+ * Decode the specified input into the corresponding actions.
44
+ *
45
+ * @param input encoded {@link JsonInput} object
46
+ * @return decoded {@link Actions} object
47
+ */
38
48
@ SuppressWarnings ("unchecked" )
39
49
public static Actions fromJson (final JsonInput input ) {
40
50
// extract list of serialized sequences
@@ -116,31 +126,73 @@ public static Actions fromJson(final JsonInput input) {
116
126
return actionsWrapper ;
117
127
}
118
128
129
+ /**
130
+ * Decode an encoded {@code pause} action.
131
+ *
132
+ * @param source input source device
133
+ * @param action encoded {@link Pause} action
134
+ * @return decoded 'pause' {@link Interaction} object
135
+ */
119
136
public static Interaction pause (final InputSource source , final Map <String , Object > action ) {
120
137
Duration duration = Duration .ofMillis ((Long ) action .get ("duration" ));
121
138
return new Pause (source , duration );
122
139
}
123
140
141
+ /**
142
+ * Decode an encoded {@code keyDown} action.
143
+ *
144
+ * @param source input source device
145
+ * @param action encoded 'keyDown' <b>TypingInteraction</b> action
146
+ * @return decoded 'keyDown' {@link Interaction} object
147
+ */
124
148
public static Interaction keyDown (final InputSource source , final Map <String , Object > action ) {
125
149
String value = action .get ("value" ).toString ();
126
150
return ((KeyInput ) source ).createKeyDown (value .codePointAt (0 ));
127
151
}
128
152
153
+ /**
154
+ * Decode an encoded {@code keyUp} action.
155
+ *
156
+ * @param source input source device
157
+ * @param action encoded 'keyUp' <b>TypingInteraction</b> action
158
+ * @return decoded 'keyUp' {@link Interaction} object
159
+ */
129
160
public static Interaction keyUp (final InputSource source , final Map <String , Object > action ) {
130
161
String value = action .get ("value" ).toString ();
131
162
return ((KeyInput ) source ).createKeyUp (value .codePointAt (0 ));
132
163
}
133
164
165
+ /**
166
+ * Decode an encoded {@code pointerDown} action.
167
+ *
168
+ * @param source input source device
169
+ * @param action encoded 'pointerDown' <b>PointerPress</b> action
170
+ * @return decoded 'pointerDown' {@link Interaction} object
171
+ */
134
172
public static Interaction pointerDown (final InputSource source , final Map <String , Object > action ) {
135
173
int button = ((Number ) action .get ("button" )).intValue ();
136
174
return ((PointerInput ) source ).createPointerDown (button , eventProperties (action ));
137
175
}
138
176
177
+ /**
178
+ * Decode an encoded {@code pointerUp} action.
179
+ *
180
+ * @param source input source device
181
+ * @param action encoded 'pointerUp' <b>PointerPress</b> action
182
+ * @return decoded 'pointerUp' {@link Interaction} object
183
+ */
139
184
public static Interaction pointerUp (final InputSource source , final Map <String , Object > action ) {
140
185
int button = ((Number ) action .get ("button" )).intValue ();
141
186
return ((PointerInput ) source ).createPointerUp (button , eventProperties (action ));
142
187
}
143
188
189
+ /**
190
+ * Decode an encoded {@code pointerMove} action.
191
+ *
192
+ * @param source input source device
193
+ * @param action encoded <b>Move</b> action
194
+ * @return decoded 'pointerMove' {@link Interaction} object
195
+ */
144
196
public static Interaction pointerMove (final InputSource source , final Map <String , Object > action ) {
145
197
Duration duration = Duration .ofMillis ((Long ) action .get ("duration" ));
146
198
Origin origin = origin (action .get ("origin" ));
@@ -149,6 +201,13 @@ public static Interaction pointerMove(final InputSource source, final Map<String
149
201
return ((PointerInput ) source ).createPointerMove (duration , origin , x , y , eventProperties (action ));
150
202
}
151
203
204
+ /**
205
+ * Decode an encoded {@code scroll} action.
206
+ *
207
+ * @param source input source device
208
+ * @param action encoded <b>ScrollInteraction</b> action
209
+ * @return decoded 'scroll' {@link Interaction} object
210
+ */
152
211
public static Interaction scroll (final InputSource source , final Map <String , Object > action ) {
153
212
Duration duration = Duration .ofMillis ((Long ) action .get ("duration" ));
154
213
ScrollOrigin origin = scrollOrigin (action .get ("origin" ));
@@ -157,6 +216,12 @@ public static Interaction scroll(final InputSource source, final Map<String, Obj
157
216
return ((WheelInput ) source ).createScroll (x , y , 0 , 0 , duration , origin );
158
217
}
159
218
219
+ /**
220
+ * Decode the specified raw properties into a pointer event properties object.
221
+ *
222
+ * @param rawProperties raw properties map
223
+ * @return decoded {@link PointerEventProperties} object
224
+ */
160
225
private static PointerEventProperties eventProperties (final Map <String , Object > rawProperties ) {
161
226
PointerEventProperties eventProperties = new PointerEventProperties ();
162
227
for (Entry <String , Object > rawProperty : rawProperties .entrySet ()) {
@@ -193,6 +258,12 @@ private static PointerEventProperties eventProperties(final Map<String, Object>
193
258
return eventProperties ;
194
259
}
195
260
261
+ /**
262
+ * Create a pointer origin object from the specified raw origin.
263
+ *
264
+ * @param rawOrigin raw origin object
265
+ * @return decoded {@link Origin} object
266
+ */
196
267
private static Origin origin (final Object rawOrigin ) {
197
268
try {
198
269
Constructor <Origin > ctor = Origin .class .getDeclaredConstructor (Object .class );
@@ -203,6 +274,12 @@ private static Origin origin(final Object rawOrigin) {
203
274
}
204
275
}
205
276
277
+ /**
278
+ * Create a scroll origin object from the specified raw origin.
279
+ *
280
+ * @param originObject raw origin object
281
+ * @return decoded {@link ScrollOrigin} object
282
+ */
206
283
private static ScrollOrigin scrollOrigin (Object originObject ) {
207
284
try {
208
285
Constructor <ScrollOrigin > ctor = ScrollOrigin .class .getDeclaredConstructor (Object .class , int .class , int .class );
@@ -213,6 +290,9 @@ private static ScrollOrigin scrollOrigin(Object originObject) {
213
290
}
214
291
}
215
292
293
+ /**
294
+ * This is a wrapper class for decoded {@link Actions} objects that facilitates element reference resolution.
295
+ */
216
296
static class ActionsWrapper extends Actions {
217
297
private WebDriverWrapper driverWrapper ;
218
298
@@ -221,23 +301,43 @@ public ActionsWrapper(WebDriverWrapper driver) {
221
301
this .driverWrapper = driver ;
222
302
}
223
303
304
+ /**
305
+ * {@inheritDoc}
306
+ */
224
307
@ Override
225
308
public Action build () {
226
309
Require .nonNull ("[ActionsWrapper] Action origins unresolved; call 'resolveOrigins' first" , driverWrapper .driver );
227
310
return super .build ();
228
311
}
229
312
313
+ /**
314
+ * Resolve origin element references of actions within this {@link Actions} object.
315
+ *
316
+ * @param driver target {@link HtmlUnitDriver}
317
+ * @return this {@link Actions} object
318
+ */
230
319
public Actions resolveOrigins (final HtmlUnitDriver driver ) {
231
320
this .driverWrapper .driver = Require .nonNull ("Driver" , driver );
232
321
resolveInteractionOriginElements (driver );
233
322
return this ;
234
323
}
235
324
325
+ /**
326
+ * Iterate over the actions of this {@link Actions} object, resolving origin element references.
327
+ *
328
+ * @param driver target {@link HtmlUnitDriver}
329
+ */
236
330
private void resolveInteractionOriginElements (final HtmlUnitDriver driver ) {
237
331
final JsonToHtmlUnitWebElementConverter elementConverter = new JsonToHtmlUnitWebElementConverter (driver );
238
332
getSequences ().stream ().map (this ::actions ).forEach (action -> elementConverter .apply (action ));
239
333
}
240
334
335
+ /**
336
+ * Obtain a reference to the list of actions within the specified sequence object.
337
+ *
338
+ * @param sequence {@link Sequence} object
339
+ * @return list of {@link Encodable} objects with the specified sequence
340
+ */
241
341
@ SuppressWarnings ("unchecked" )
242
342
private List <Encodable > actions (final Sequence sequence ) {
243
343
try {
@@ -250,79 +350,127 @@ private List<Encodable> actions(final Sequence sequence) {
250
350
}
251
351
}
252
352
353
+ /**
354
+ * This wrapper class provides placeholder objects for the drivers used by {@link ActionsCoercer} objects.
355
+ */
253
356
private static class WebDriverWrapper implements WebDriver , Interactive {
254
357
private WebDriver driver ;
255
358
359
+ /**
360
+ * {@inheritDoc}
361
+ */
256
362
@ Override
257
363
public void perform (Collection <Sequence > actions ) {
258
364
((Interactive ) driver ).perform (actions );
259
365
}
260
366
367
+ /**
368
+ * {@inheritDoc}
369
+ */
261
370
@ Override
262
371
public void resetInputState () {
263
372
((Interactive ) driver ).resetInputState ();
264
373
}
265
374
375
+ /**
376
+ * {@inheritDoc}
377
+ */
266
378
@ Override
267
379
public void get (String url ) {
268
380
driver .get (url );
269
381
}
270
382
383
+ /**
384
+ * {@inheritDoc}
385
+ */
271
386
@ Override
272
387
public String getCurrentUrl () {
273
388
return driver .getCurrentUrl ();
274
389
}
275
390
391
+ /**
392
+ * {@inheritDoc}
393
+ */
276
394
@ Override
277
395
public String getTitle () {
278
396
return driver .getTitle ();
279
397
}
280
398
399
+ /**
400
+ * {@inheritDoc}
401
+ */
281
402
@ Override
282
403
public List <WebElement > findElements (By by ) {
283
404
return driver .findElements (by );
284
405
}
285
406
407
+ /**
408
+ * {@inheritDoc}
409
+ */
286
410
@ Override
287
411
public WebElement findElement (By by ) {
288
412
return driver .findElement (by );
289
413
}
290
414
415
+ /**
416
+ * {@inheritDoc}
417
+ */
291
418
@ Override
292
419
public String getPageSource () {
293
420
return driver .getPageSource ();
294
421
}
295
422
423
+ /**
424
+ * {@inheritDoc}
425
+ */
296
426
@ Override
297
427
public void close () {
298
428
driver .close ();
299
429
}
300
430
431
+ /**
432
+ * {@inheritDoc}
433
+ */
301
434
@ Override
302
435
public void quit () {
303
436
driver .quit ();
304
437
}
305
438
439
+ /**
440
+ * {@inheritDoc}
441
+ */
306
442
@ Override
307
443
public Set <String > getWindowHandles () {
308
444
return driver .getWindowHandles ();
309
445
}
310
446
447
+ /**
448
+ * {@inheritDoc}
449
+ */
311
450
@ Override
312
451
public String getWindowHandle () {
313
452
return driver .getWindowHandle ();
314
453
}
315
454
455
+ /**
456
+ * {@inheritDoc}
457
+ */
316
458
@ Override
317
459
public TargetLocator switchTo () {
318
460
return driver .switchTo ();
319
461
}
320
462
463
+ /**
464
+ * {@inheritDoc}
465
+ */
321
466
@ Override
322
467
public Navigation navigate () {
323
468
return driver .navigate ();
324
469
}
325
470
471
+ /**
472
+ * {@inheritDoc}
473
+ */
326
474
@ Override
327
475
public Options manage () {
328
476
return driver .manage ();
0 commit comments