Action Class in Selenium is a built-in feature offered by selenium for managing keyboard and mouse events. It can manage multiple operations and events such as control key, drag and drop, and many more. The Action Class’s operations are implemented using the advanced user interaction API feature of the Selenium Webdriver.
Commonly used Keyboard and Mouse Events
Special keyboard and mouse events are handled using the Advanced User Interactions API. This API contains the Action and the Actions class that are required while executing these events. Given below are the commonly used keyboard and mouse events provided in the Action class.
|Click And Hold()||To click without releasing at the current mouse location.|
|context-click()||Performs a context-click(Right-click mouse action) at the current mouse location|
|doubleClick()||Performs a double-click action at the current mouse location|
|drag and drop(source, target)||Performs click-and-hold operation at the source element location, then moves to the target element location and releases the mouse.
source-element to emulate button-down events.
Target-element to move and release the mouse
|dragAnd DropBy(source, x-offset, y-offset)||Performs click-and-hold at the source element location, moves by a specified offset and then releases the mouse.
source-element to emulate button-down events.
xOffset- to specify horizontal move offset.
yOffset- to specify vertical move offset.
|keyDown(modifier_key)||Performs the modifier keypress event. Does not release the modifier key with subsequent interactions assuming that it is pressed.
modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT, or Keys.CONTROL.
|key Up(modifier_key)||Performs a key release.
modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT or Keys.CONTROL.
|moveByOffset(x-offset, y-offset)||Moves the mouse from the current position by the specified offset.
x-offset to specify the horizontal offset. A negative value means the mouse is moving to the left.
y-offset to specify the vertical offset. A negative value means the mouse is moving downwards.
|moveToElement(toElement)||Moves the mouse from the middle to the middle of the element.
element to represent the element to move to.
|release()||Releases the pressed mouse left mouse button at the current mouse location.|
|sendKeys(onElement, charsequence)||Sends a series of keystrokes on to the element.
Element to represent the element that will get the keystrokes, usually a text field.
Char sequence- any string value to represent the sequence of keystrokes that is sent.
Examples of Application of the Selenium Action Class
1. Import Action and the Action class.
import org.open a.selenium.interactions.Action;
import org.open a.selenium.interactions.Actions;
2. Instantiate a new Actions object
Actions builder = new Actions(driver);
3. Instantiate an Action using the Action object in the previous step
Action mouseOverHome = builder
Here, we are using the moveToElement() method to perform a mouse-over the Home link. The build() is always the final method to ensure that all actions are compiled in a single step.
4. Use the perform method while executing the Action object we declared in the previous step.
Let us take another example where the Action class is used to implement the textbox’s Autocomplete feature.
To do this manually, we press the shift key, type the text that must be entered in uppercase and then let go of the shift key. Put, Shift + alphabet keys are pressed together.
To mimic the same operation through automation script, the Actions Class method is utilized.
Action and Actions class exist in the org.openqa.selenium.interactions package of the Web Driver API. To use these you must import packages:
2. Instantiate the Action class
It would help if you created an object of the Actions class to invoke its methods. So, we instantiate the Actions class and use the WebDriver object to initiate the class. The syntax is:
Actions actions = new Actions (webdriver object);
3. Generate a sequence of actions
In this example, the sequence of steps to perform multiple actions are:
- Press the Shift key
- Send the desired text
- Release the Shift key
To perform these actions, the Action class has methods such as:
- keyDown method to press the Shift key.
- sendKeys method to send the desired text.
- The keyUp method releases the Shift key.
- The keyDown acts as a modifier key press to focus on an element while the keyUp method releases a modifier key pressed.
A modifier key changes the action of another key when both the keys are pressed simultaneously. E.g., Shift, Alt and Ctrl.
A sequence of actions is generated but these actions are performed on a webElement. The steps for this are:
WebElement element = driver.findElement;
actions.keyDown(element, Keys, Shift);
The important thing to observe here is that if you happen to hover around any Action class method, an Actions class object will be returned.
4. Build the actions sequence
You can build the actions sequence through the build() method of the Actions class. This method generates the entire set of actions that are ready to be performed. The syntax is:
Action action = actions.build();
The build() method returns an object of type Action that represents a composite action built from a sequence of multiple actions invoked by method calls.
5. Perform action sequences
The last step in executing the Action class is performing the actions sequence through the perform() method of the Action interface. The syntax is:
The above-given steps must be executed to leverage all of the Actions class methods and perform different user actions using the keyboard and mouse.
If you’re interested to learn more about Full-Stack Software development, check out upGrad & IIIT-B’s Executive PG Program Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.