org.openqa.selenium.interactions.KeyInput Java Examples

The following examples show how to use org.openqa.selenium.interactions.KeyInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Edition046_W3C_Keys.java    From appiumpro with Apache License 2.0 6 votes vote down vote up
@Test
public void testLowLevelKeys() {
    wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click();
    WebElement usernameField = driver.findElement(username);
    usernameField.click();

    KeyInput keyboard = new KeyInput("keyboard");
    Sequence sendKeys = new Sequence(keyboard, 0);

    sendKeys.addAction(keyboard.createKeyDown(Keys.SHIFT.getCodePoint()));
    sendKeys.addAction(keyboard.createKeyDown("f".codePointAt(0)));
    sendKeys.addAction(keyboard.createKeyUp("f".codePointAt(0)));
    sendKeys.addAction(keyboard.createKeyUp(Keys.SHIFT.getCodePoint()));

    sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0)));
    sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0)));

    sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0)));
    sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0)));

    driver.perform(Arrays.asList(sendKeys));

    Assert.assertEquals("Foo", usernameField.getText());
}
 
Example #2
Source File: AppiumW3CHttpCommandCodec.java    From java-client with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<String, ?> amendParameters(String name, Map<String, ?> parameters) {
    // This blocks parent constructor from undesirable parameters amending
    switch (name) {
        case SEND_KEYS_TO_ACTIVE_ELEMENT:
            Object rawValue = parameters.get("value");
            //noinspection unchecked
            Stream<CharSequence> source = (rawValue instanceof Collection)
                    ? ((Collection<CharSequence>) rawValue).stream()
                    : Stream.of((CharSequence[]) rawValue);
            String text = source
                    .flatMap(Stream::of)
                    .collect(Collectors.joining());

            final KeyInput keyboard = new KeyInput("keyboard");
            Sequence sequence = new Sequence(keyboard, 0);
            for (int i = 0; i < text.length(); ++i) {
                sequence.addAction(keyboard.createKeyDown(text.charAt(i)))
                        .addAction(keyboard.createKeyUp(text.charAt(i)));
            }
            return ImmutableMap.<String, Object>builder()
                    .put("actions", ImmutableList.of(sequence.toJson()))
                    .build();
        case SEND_KEYS_TO_ELEMENT:
        case SET_TIMEOUT:
            return super.amendParameters(name, parameters);
        default:
            return parameters;
    }
}