How to use pressKey method of org.openqa.selenium.interactions.Interface Keyboard class

Best Selenium code snippet using org.openqa.selenium.interactions.Interface Keyboard.pressKey

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Applitools SDK for Selenium integration.
3 */
4package com.applitools.eyes;
5
6import com.applitools.utils.ArgumentGuard;
7import org.openqa.selenium.interactions.Keyboard;
8import org.openqa.selenium.WebElement;
9import org.openqa.selenium.remote.RemoteWebElement;
10
11/**
12 * A wrapper class for Selenium's Keyboard interface, so we can record keyboard
13 * events.
14 */
15class EyesKeyboard implements Keyboard {
16
17    private final Logger logger;
18    private final EyesWebDriver eyesDriver;
19    private final Keyboard keyboard;
20
21    public EyesKeyboard(Logger logger, EyesWebDriver eyesDriver,
22                        Keyboard keyboard) {
23        ArgumentGuard.notNull(logger, "logger");
24        ArgumentGuard.notNull(eyesDriver, "eyesDriver");
25        ArgumentGuard.notNull(keyboard, "keyboard");
26
27        this.logger = logger;
28        this.eyesDriver = eyesDriver;
29        this.keyboard = keyboard;
30    }
31
32    public void sendKeys(CharSequence... charSequences) {
33
34        Region control = Region.EMPTY;
35
36        // We first find the active element to get the region
37        WebElement activeElement = eyesDriver.switchTo().activeElement();
38
39        if (activeElement instanceof RemoteWebElement) {
40            activeElement = new EyesRemoteWebElement(logger, eyesDriver,
41                    (RemoteWebElement) activeElement);
42
43            control = ((EyesRemoteWebElement)activeElement).getBounds();
44        }
45
46        for(CharSequence keys : charSequences) {
47            String text = String.valueOf(keys);
48            eyesDriver.getEyes().addTextTrigger(control, text);
49        }
50
51        keyboard.sendKeys(charSequences);
52    }
53
54    public void pressKey(CharSequence keyToPress) {
55        keyboard.pressKey(keyToPress);
56    }
57
58    public void releaseKey(CharSequence keyToRelease) {
59        keyboard.releaseKey(keyToRelease);
60    }
61}
62
Full Screen
copy
1package org.openqa.selenium.interactions;
2
3public abstract interface Keyboard
4{
5  public abstract void sendKeys(CharSequence... paramVarArgs);
6  
7  public abstract void pressKey(CharSequence paramCharSequence);
8  
9  public abstract void releaseKey(CharSequence paramCharSequence);
10}
11
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Interface-Keyboard

Trigger pressKey code on LambdaTest Cloud Grid

Execute automation tests with pressKey on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie