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

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

Run Selenium automation tests on LambdaTest cloud grid

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

copy
1package org.fluentlenium.core.action;
2
3import org.openqa.selenium.Keys;
4import org.openqa.selenium.WebDriver;
5import org.openqa.selenium.WebElement;
6import org.openqa.selenium.interactions.HasInputDevices;
7import org.openqa.selenium.interactions.Keyboard;
8
9/**
10 * Execute actions with the keyboard.
11 */
12public class KeyboardActions {
13    private final WebDriver driver;
14
15    /**
16     * Creates a new object to execute actions with the keyboard, using given selenium driver.
17     *
18     * @param driver selenium driver
19     */
20    public KeyboardActions(WebDriver driver) {
21        this.driver = driver;
22    }
23
24    /**
25     * Get selenium interactions actions.
26     *
27     * @return selenium actions
28     */
29    protected org.openqa.selenium.interactions.Actions actions() {
30        return new org.openqa.selenium.interactions.Actions(driver);
31    }
32
33    /**
34     * Basic keyboard operations
35     *
36     * @return low level interface to control the keyboard
37     * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}
38     * and {@link KeyboardActions#sendKeys(CharSequence...)} instead
39     */
40    @Deprecated
41    public Keyboard basic() {
42        return ((HasInputDevices) driver).getKeyboard();
43    }
44
45    /**
46     * Performs a modifier key press. Does not release the modifier key - subsequent interactions
47     * may assume it's kept pressed.
48     * Note that the modifier key is <b>never</b> released implicitly - either
49     * <i>keyUp(theKey)</i> or <i>sendKeys(Keys.NULL)</i>
50     * must be called to release the modifier.
51     *
52     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the
53     *               provided key is none of those, {@link IllegalArgumentException} is thrown.
54     * @return this object reference to chain calls
55     * @see org.openqa.selenium.interactions.Actions#keyDown(CharSequence)
56     */
57    public KeyboardActions keyDown(Keys theKey) {
58        actions().keyDown(theKey).perform();
59        return this;
60    }
61
62    /**
63     * Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined
64     * behaviour.
65     *
66     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.
67     * @return this object reference to chain calls
68     * @see org.openqa.selenium.interactions.Actions#keyUp(CharSequence)
69     */
70    public KeyboardActions keyUp(Keys theKey) {
71        actions().keyUp(theKey).perform();
72        return this;
73    }
74
75    /**
76     * Sends keys to the active element. This differs from calling
77     * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:
78     * <ul>
79     * <li>The modifier keys included in this call are not released.</li>
80     * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching
81     * elements should work. </li>
82     * </ul>
83     *
84     * @param keysToSend The keys.
85     * @return A self reference.
86     * @see org.openqa.selenium.interactions.Actions#sendKeys(CharSequence...)
87     */
88    public KeyboardActions sendKeys(CharSequence... keysToSend) {
89        actions().sendKeys(keysToSend).perform();
90        return this;
91    }
92}
93
Full Screen
copy
1package org.fluentlenium.core.action;
2
3import org.fluentlenium.core.domain.FluentWebElement;
4import org.openqa.selenium.Keys;
5import org.openqa.selenium.WebDriver;
6import org.openqa.selenium.WebElement;
7import org.openqa.selenium.interactions.HasInputDevices;
8import org.openqa.selenium.interactions.Keyboard;
9
10/**
11 * Execute actions with the keyboard on a defined element.
12 */
13public class KeyboardElementActions {
14    private final WebDriver driver;
15    private final WebElement element;
16
17    /**
18     * Creates a new object to execute actions with the keyboard, using given selenium driver and element.
19     *
20     * @param driver  selenium driver
21     * @param element element on which to execute actions
22     */
23    public KeyboardElementActions(WebDriver driver, WebElement element) {
24        this.driver = driver;
25        this.element = element;
26    }
27
28    /**
29     * Creates a new object to execute actions with the keyboard, using given selenium driver and element.
30     *
31     * @param driver  selenium driver
32     * @param fluentWebElement FluentWebElement on which to execute actions
33     */
34    public KeyboardElementActions(WebDriver driver, FluentWebElement fluentWebElement) {
35        this.driver = driver;
36        this.element = fluentWebElement.getElement();
37    }
38
39    /**
40     * Get selenium interactions actions.
41     *
42     * @return selenium actions
43     */
44    private org.openqa.selenium.interactions.Actions actions() {
45        return new org.openqa.selenium.interactions.Actions(driver);
46    }
47
48    /**
49     * Basic keyboard operations
50     *
51     * @return low level interface to control the keyboard
52     * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}
53     * and {@link KeyboardActions#sendKeys(CharSequence...)} instead
54     */
55    @Deprecated
56    public Keyboard basic() {
57        return ((HasInputDevices) driver).getKeyboard();
58    }
59
60    /**
61     * Performs a modifier key press after focusing on an element. Equivalent to:
62     * <i>Actions.click(element).sendKeys(theKey);</i>
63     *
64     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the
65     *               provided key is none of those, {@link IllegalArgumentException} is thrown.
66     * @return this object reference to chain calls
67     * @see #keyDown(org.openqa.selenium.Keys)
68     * @see org.openqa.selenium.interactions.Actions#keyDown(WebElement, CharSequence)
69     */
70    public KeyboardElementActions keyDown(Keys theKey) {
71        actions().keyDown(element, theKey).perform();
72        return this;
73    }
74
75    /**
76     * Performs a modifier key release after focusing on an element. Equivalent to:
77     * <i>Actions.click(element).sendKeys(theKey);</i>
78     *
79     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.
80     * @return this object reference to chain calls
81     * @see org.openqa.selenium.interactions.Actions#keyUp(WebElement, CharSequence)
82     */
83    public KeyboardElementActions keyUp(Keys theKey) {
84        actions().keyUp(element, theKey).perform();
85        return this;
86    }
87
88    /**
89     * Sends keys to the active element. This differs from calling
90     * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:
91     * <ul>
92     * <li>The modifier keys included in this call are not released.</li>
93     * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching
94     * elements should work. </li>
95     * </ul>
96     *
97     * @param keysToSend The keys.
98     * @return this object reference to chain calls
99     * @see org.openqa.selenium.interactions.Actions#sendKeys(WebElement, CharSequence...)
100     */
101    public KeyboardElementActions sendKeys(CharSequence... keysToSend) {
102        actions().sendKeys(element, keysToSend).perform();
103        return this;
104    }
105}
106
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 sendKeys code on LambdaTest Cloud Grid

Execute automation tests with sendKeys 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