3535import java .util .HashSet ;
3636import java .util .LinkedHashMap ;
3737import java .util .Map ;
38+ import java .util .Objects ;
3839import java .util .Set ;
3940import java .util .function .IntConsumer ;
4041import java .util .logging .Logger ;
@@ -55,8 +56,8 @@ public class Actions {
5556
5657 // W3C
5758 private final Map <InputSource , Sequence > sequences = new HashMap <>();
58- private final PointerInput defaultMouse = new PointerInput ( MOUSE , "default mouse" ) ;
59- private final KeyInput defaultKeyboard = new KeyInput ( "default keyboard" ) ;
59+ private PointerInput activePointer ;
60+ private KeyInput activeKeyboard ;
6061
6162 // JSON-wire protocol
6263 private final Keyboard jsonKeyboard ;
@@ -90,7 +91,7 @@ public Actions keyDown(CharSequence key) {
9091 if (isBuildingActions ()) {
9192 action .addAction (new KeyDownAction (jsonKeyboard , jsonMouse , asKeys (key )));
9293 }
93- return addKeyAction (key , codePoint -> tick (defaultKeyboard .createKeyDown (codePoint )));
94+ return addKeyAction (key , codePoint -> tick (getActiveKeyboard () .createKeyDown (codePoint )));
9495 }
9596
9697 /**
@@ -108,7 +109,7 @@ public Actions keyDown(WebElement target, CharSequence key) {
108109 action .addAction (new KeyDownAction (jsonKeyboard , jsonMouse , (Locatable ) target , asKeys (key )));
109110 }
110111 return focusInTicks (target )
111- .addKeyAction (key , codepoint -> tick (defaultKeyboard .createKeyDown (codepoint )));
112+ .addKeyAction (key , codepoint -> tick (getActiveKeyboard () .createKeyDown (codepoint )));
112113 }
113114
114115 /**
@@ -123,7 +124,7 @@ public Actions keyUp(CharSequence key) {
123124 action .addAction (new KeyUpAction (jsonKeyboard , jsonMouse , asKeys (key )));
124125 }
125126
126- return addKeyAction (key , codePoint -> tick (defaultKeyboard .createKeyUp (codePoint )));
127+ return addKeyAction (key , codePoint -> tick (getActiveKeyboard () .createKeyUp (codePoint )));
127128 }
128129
129130 /**
@@ -141,7 +142,7 @@ public Actions keyUp(WebElement target, CharSequence key) {
141142 }
142143
143144 return focusInTicks (target )
144- .addKeyAction (key , codePoint -> tick (defaultKeyboard .createKeyUp (codePoint )));
145+ .addKeyAction (key , codePoint -> tick (getActiveKeyboard () .createKeyUp (codePoint )));
145146 }
146147
147148 /**
@@ -205,8 +206,8 @@ private Actions sendKeysInTicks(CharSequence... keys) {
205206 }
206207 for (CharSequence key : keys ) {
207208 key .codePoints ().forEach (codePoint -> {
208- tick (defaultKeyboard .createKeyDown (codePoint ));
209- tick (defaultKeyboard .createKeyUp (codePoint ));
209+ tick (getActiveKeyboard () .createKeyDown (codePoint ));
210+ tick (getActiveKeyboard () .createKeyUp (codePoint ));
210211 });
211212 }
212213 return this ;
@@ -236,7 +237,7 @@ public Actions clickAndHold(WebElement target) {
236237 action .addAction (new ClickAndHoldAction (jsonMouse , (Locatable ) target ));
237238 }
238239 return moveInTicks (target , 0 , 0 )
239- .tick (defaultMouse .createPointerDown (LEFT .asArg ()));
240+ .tick (getActivePointer () .createPointerDown (LEFT .asArg ()));
240241 }
241242
242243 /**
@@ -248,7 +249,7 @@ public Actions clickAndHold() {
248249 action .addAction (new ClickAndHoldAction (jsonMouse , null ));
249250 }
250251
251- return tick (defaultMouse .createPointerDown (LEFT .asArg ()));
252+ return tick (getActivePointer () .createPointerDown (LEFT .asArg ()));
252253 }
253254
254255 /**
@@ -267,7 +268,7 @@ public Actions release(WebElement target) {
267268 action .addAction (new ButtonReleaseAction (jsonMouse , (Locatable ) target ));
268269 }
269270
270- return moveInTicks (target , 0 , 0 ).tick (defaultMouse .createPointerUp (LEFT .asArg ()));
271+ return moveInTicks (target , 0 , 0 ).tick (getActivePointer () .createPointerUp (LEFT .asArg ()));
271272 }
272273
273274 /**
@@ -280,7 +281,7 @@ public Actions release() {
280281 action .addAction (new ButtonReleaseAction (jsonMouse , null ));
281282 }
282283
283- return tick (defaultMouse .createPointerUp (Button .LEFT .asArg ()));
284+ return tick (getActivePointer () .createPointerUp (Button .LEFT .asArg ()));
284285 }
285286
286287 /**
@@ -313,8 +314,8 @@ public Actions click() {
313314 }
314315
315316 private Actions clickInTicks (PointerInput .MouseButton button ) {
316- tick (defaultMouse .createPointerDown (button .asArg ()));
317- tick (defaultMouse .createPointerUp (button .asArg ()));
317+ tick (getActivePointer () .createPointerDown (button .asArg ()));
318+ tick (getActivePointer () .createPointerUp (button .asArg ()));
318319 return this ;
319320 }
320321
@@ -386,7 +387,7 @@ public Actions moveToElement(WebElement target, int xOffset, int yOffset) {
386387 }
387388
388389 private Actions moveInTicks (WebElement target , int xOffset , int yOffset ) {
389- return tick (defaultMouse .createPointerMove (
390+ return tick (getActivePointer () .createPointerMove (
390391 Duration .ofMillis (100 ),
391392 Origin .fromElement (target ),
392393 xOffset ,
@@ -409,7 +410,7 @@ public Actions moveByOffset(int xOffset, int yOffset) {
409410 }
410411
411412 return tick (
412- defaultMouse .createPointerMove (Duration .ofMillis (200 ), Origin .pointer (), xOffset , yOffset ));
413+ getActivePointer () .createPointerMove (Duration .ofMillis (200 ), Origin .pointer (), xOffset , yOffset ));
413414 }
414415
415416 /**
@@ -454,9 +455,9 @@ public Actions dragAndDrop(WebElement source, WebElement target) {
454455 }
455456
456457 return moveInTicks (source , 0 , 0 )
457- .tick (defaultMouse .createPointerDown (LEFT .asArg ()))
458+ .tick (getActivePointer () .createPointerDown (LEFT .asArg ()))
458459 .moveInTicks (target , 0 , 0 )
459- .tick (defaultMouse .createPointerUp (LEFT .asArg ()));
460+ .tick (getActivePointer () .createPointerUp (LEFT .asArg ()));
460461 }
461462
462463 /**
@@ -476,9 +477,9 @@ public Actions dragAndDropBy(WebElement source, int xOffset, int yOffset) {
476477 }
477478
478479 return moveInTicks (source , 0 , 0 )
479- .tick (defaultMouse .createPointerDown (LEFT .asArg ()))
480- .tick (defaultMouse .createPointerMove (Duration .ofMillis (250 ), Origin .pointer (), xOffset , yOffset ))
481- .tick (defaultMouse .createPointerUp (LEFT .asArg ()));
480+ .tick (getActivePointer () .createPointerDown (LEFT .asArg ()))
481+ .tick (getActivePointer () .createPointerMove (Duration .ofMillis (250 ), Origin .pointer (), xOffset , yOffset ))
482+ .tick (getActivePointer () .createPointerUp (LEFT .asArg ()));
482483 }
483484
484485 /**
@@ -492,7 +493,7 @@ public Actions pause(long pause) {
492493 action .addAction (new PauseAction (pause ));
493494 }
494495
495- return tick (new Pause (defaultMouse , Duration .ofMillis (pause )));
496+ return tick (new Pause (getActivePointer () , Duration .ofMillis (pause )));
496497 }
497498
498499 public Actions pause (Duration duration ) {
@@ -501,7 +502,7 @@ public Actions pause(Duration duration) {
501502 action .addAction (new PauseAction (duration .toMillis ()));
502503 }
503504
504- return tick (new Pause (defaultMouse , duration ));
505+ return tick (new Pause (getActivePointer () , duration ));
505506 }
506507
507508 public Actions tick (Interaction ... actions ) {
@@ -538,7 +539,7 @@ public Actions tick(Action action) {
538539 }
539540
540541 for (Interaction interaction :
541- ((IsInteraction ) action ).asInteractions (defaultMouse , defaultKeyboard )) {
542+ ((IsInteraction ) action ).asInteractions (getActivePointer (), getActiveKeyboard () )) {
542543 tick (interaction );
543544 }
544545
@@ -549,6 +550,44 @@ public Actions tick(Action action) {
549550 return this ;
550551 }
551552
553+ public Actions setActiveKeyboard (String name ) {
554+ InputSource inputSource = sequences .keySet ().stream ().filter (input -> Objects .equals (input .getName (), name )).findFirst ().orElse (null );
555+
556+ if (inputSource == null ) {
557+ this .activeKeyboard = new KeyInput (name );
558+ } else {
559+ this .activeKeyboard = (KeyInput ) inputSource ;
560+ }
561+
562+ return this ;
563+ }
564+
565+ public Actions setActivePointer (PointerInput .Kind kind , String name ) {
566+ InputSource inputSource = sequences .keySet ().stream ().filter (input -> Objects .equals (input .getName (), name )).findFirst ().orElse (null );
567+
568+ if (inputSource == null ) {
569+ this .activePointer = new PointerInput (kind , name );
570+ } else {
571+ this .activePointer = (PointerInput ) inputSource ;
572+ }
573+
574+ return this ;
575+ }
576+
577+ public KeyInput getActiveKeyboard () {
578+ if (this .activeKeyboard == null ) {
579+ setActiveKeyboard ("default keyboard" );
580+ }
581+ return this .activeKeyboard ;
582+ }
583+
584+ public PointerInput getActivePointer () {
585+ if (this .activePointer == null ) {
586+ setActivePointer (PointerInput .Kind .MOUSE , "default mouse" );
587+ }
588+ return this .activePointer ;
589+ }
590+
552591 /**
553592 * Generates a composite action containing all actions so far, ready to be performed (and
554593 * resets the internal builder state, so subsequent calls to this method will contain fresh
0 commit comments