2727import static org .openqa .selenium .remote .DriverCommand .SET_TIMEOUT ;
2828import static org .openqa .selenium .remote .DriverCommand .SUBMIT_ELEMENT ;
2929
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 ;
3035import org .openqa .selenium .remote .http .W3CHttpCommandCodec ;
3136
37+ import java .util .Collection ;
3238import java .util .Map ;
39+ import java .util .stream .Collectors ;
40+ import java .util .stream .Stream ;
3341
3442public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec {
35-
3643 /**
3744 * This class overrides the built-in Selenium W3C commands codec,
3845 * since the latter hardcodes many commands in Javascript,
@@ -44,6 +51,7 @@ public AppiumW3CHttpCommandCodec() {
4451 defineCommand (GET_ELEMENT_ATTRIBUTE , get ("/session/:sessionId/element/:id/attribute/:name" ));
4552 defineCommand (IS_ELEMENT_DISPLAYED , get ("/session/:sessionId/element/:id/displayed" ));
4653 defineCommand (GET_PAGE_SOURCE , get ("/session/:sessionId/source" ));
54+ defineCommand (SEND_KEYS_TO_ACTIVE_ELEMENT , post ("/session/:sessionId/actions" ));
4755 }
4856
4957 @ Override
@@ -69,6 +77,24 @@ public void alias(String commandName, String isAnAliasFor) {
6977 // This blocks parent constructor from undesirable parameters amending
7078 switch (name ) {
7179 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 ();
7298 case SEND_KEYS_TO_ELEMENT :
7399 case SET_TIMEOUT :
74100 return super .amendParameters (name , parameters );
0 commit comments