27
27
import static org .openqa .selenium .remote .DriverCommand .SET_TIMEOUT ;
28
28
import static org .openqa .selenium .remote .DriverCommand .SUBMIT_ELEMENT ;
29
29
30
+ import com .google .common .collect .ImmutableList ;
31
+ import com .google .common .collect .ImmutableMap ;
32
+
33
+ import org .openqa .selenium .interactions .KeyInput ;
34
+ import org .openqa .selenium .interactions .Sequence ;
30
35
import org .openqa .selenium .remote .http .W3CHttpCommandCodec ;
31
36
37
+ import java .util .Collection ;
32
38
import java .util .Map ;
39
+ import java .util .stream .Collectors ;
40
+ import java .util .stream .Stream ;
33
41
34
42
public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec {
35
-
36
43
/**
37
44
* This class overrides the built-in Selenium W3C commands codec,
38
45
* since the latter hardcodes many commands in Javascript,
@@ -44,6 +51,7 @@ public AppiumW3CHttpCommandCodec() {
44
51
defineCommand (GET_ELEMENT_ATTRIBUTE , get ("/session/:sessionId/element/:id/attribute/:name" ));
45
52
defineCommand (IS_ELEMENT_DISPLAYED , get ("/session/:sessionId/element/:id/displayed" ));
46
53
defineCommand (GET_PAGE_SOURCE , get ("/session/:sessionId/source" ));
54
+ defineCommand (SEND_KEYS_TO_ACTIVE_ELEMENT , post ("/session/:sessionId/actions" ));
47
55
}
48
56
49
57
@ Override
@@ -69,6 +77,24 @@ public void alias(String commandName, String isAnAliasFor) {
69
77
// This blocks parent constructor from undesirable parameters amending
70
78
switch (name ) {
71
79
case SEND_KEYS_TO_ACTIVE_ELEMENT :
80
+ Object rawValue = parameters .get ("value" );
81
+ //noinspection unchecked
82
+ Stream <CharSequence > source = (rawValue instanceof Collection )
83
+ ? ((Collection <CharSequence >) rawValue ).stream ()
84
+ : Stream .of ((CharSequence []) rawValue );
85
+ String text = source
86
+ .flatMap (Stream ::of )
87
+ .collect (Collectors .joining ());
88
+
89
+ final KeyInput keyboard = new KeyInput ("keyboard" );
90
+ Sequence sequence = new Sequence (keyboard , 0 );
91
+ for (int i = 0 ; i < text .length (); ++i ) {
92
+ sequence .addAction (keyboard .createKeyDown (text .charAt (i )))
93
+ .addAction (keyboard .createKeyUp (text .charAt (i )));
94
+ }
95
+ return ImmutableMap .<String , Object >builder ()
96
+ .put ("actions" , ImmutableList .of (sequence .toJson ()))
97
+ .build ();
72
98
case SEND_KEYS_TO_ELEMENT :
73
99
case SET_TIMEOUT :
74
100
return super .amendParameters (name , parameters );
0 commit comments