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