Skip to content

Commit 541eca8

Browse files
author
Mykola Mokhnach
authored
Map sending keys to active element for W3C compatibility (#966)
* Map sending keys to active element for W3C compatibility * fix linter
1 parent d8e477c commit 541eca8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@
2727
import static org.openqa.selenium.remote.DriverCommand.SET_TIMEOUT;
2828
import 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;
3035
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
3136

37+
import java.util.Collection;
3238
import java.util.Map;
39+
import java.util.stream.Collectors;
40+
import java.util.stream.Stream;
3341

3442
public 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

Comments
 (0)