.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)));
@Override public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) { List<Interaction> interactions = new ArrayList<>(optionallyClickElement(mouse)); for (CharSequence keys : keysToSend) { keys.codePoints().forEach(codePoint -> { interactions.add(keyboard.createKeyDown(codePoint)); interactions.add(keyboard.createKeyUp(codePoint)); }); } return Collections.unmodifiableList(interactions); } }
@Override public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) { ArrayList<Interaction> interactions = new ArrayList<>(); interactions.addAll(optionallyClickElement(mouse)); interactions.add(keyboard.createKeyDown(key.getCodePoint())); return Collections.unmodifiableList(interactions); } }
@Override public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) { List<Interaction> toReturn = new ArrayList<>(optionallyClickElement(mouse)); toReturn.add(keyboard.createKeyUp(key.getCodePoint())); return Collections.unmodifiableList(toReturn); }
@Override public Map<String, Object> encode() { Map<String, Object> toReturn = new HashMap<>(); toReturn.put("type", getInputType().getType()); toReturn.put("id", name); return toReturn; }
private Actions sendKeysInTicks(CharSequence... keys) { if (keys == null) { throw new IllegalArgumentException("Keys should be a not null CharSequence"); } for (CharSequence key : keys) { key.codePoints().forEach(codePoint -> { tick(defaultKeyboard.createKeyDown(codePoint)); tick(defaultKeyboard.createKeyUp(codePoint)); }); } return this; }
/** * Performs a modifier key press. Does not release the modifier key - subsequent interactions * may assume it's kept pressed. * Note that the modifier key is <b>never</b> released implicitly - either * <i>keyUp(theKey)</i> or <i>sendKeys(Keys.NULL)</i> * must be called to release the modifier. * @param key Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the * provided key is none of those, {@link IllegalArgumentException} is thrown. * @return A self reference. */ public Actions keyDown(CharSequence key) { if (isBuildingActions()) { action.addAction(new KeyDownAction(jsonKeyboard, jsonMouse, asKeys(key))); } return addKeyAction(key, codePoint -> tick(defaultKeyboard.createKeyDown(codePoint))); }
/** * Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined * behaviour. * * @param key Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. * @return A self reference. */ public Actions keyUp(CharSequence key) { if (isBuildingActions()) { action.addAction(new KeyUpAction(jsonKeyboard, jsonMouse, asKeys(key))); } return addKeyAction(key, codePoint -> tick(defaultKeyboard.createKeyUp(codePoint))); }
.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)));
/** * Performs a modifier key press after focusing on an element. Equivalent to: * <i>Actions.click(element).sendKeys(theKey);</i> * @see #keyDown(CharSequence) * * @param key Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the * provided key is none of those, {@link IllegalArgumentException} is thrown. * @param target WebElement to perform the action * @return A self reference. */ public Actions keyDown(WebElement target, CharSequence key) { if (isBuildingActions()) { action.addAction(new KeyDownAction(jsonKeyboard, jsonMouse, (Locatable) target, asKeys(key))); } return focusInTicks(target) .addKeyAction(key, codepoint -> tick(defaultKeyboard.createKeyDown(codepoint))); }
/** * Performs a modifier key release after focusing on an element. Equivalent to: * <i>Actions.click(element).sendKeys(theKey);</i> * @see #keyUp(CharSequence) on behaviour regarding non-depressed modifier keys. * * @param key Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. * @param target WebElement to perform the action on * @return A self reference. */ public Actions keyUp(WebElement target, CharSequence key) { if (isBuildingActions()) { action.addAction(new KeyUpAction(jsonKeyboard, jsonMouse, (Locatable) target, asKeys(key))); } return focusInTicks(target) .addKeyAction(key, codePoint -> tick(defaultKeyboard.createKeyUp(codePoint))); }
@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()); }