What is Action Class in Selenium & It’s Applications?
Updated on Nov 18, 2022 | 7 min read | 7.2k views
Share:
For working professionals
For fresh graduates
More
Updated on Nov 18, 2022 | 7 min read | 7.2k views
Share:
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.
Check out our free courses to get an edge over the competition.
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.
Method | Description |
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. Parameters: 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. Parameters: 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. Parameters: 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. Parameters: 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. Parameters: 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. Parameters: 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. Parameters: 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. |
import org.open a.selenium.interactions.Action;
import org.open a.selenium.interactions.Actions;
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(link_Home)
.build();
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.
Check out upGrad’s Java Bootcamp.
Syntax:
mouseOverHome.perform();
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:
org.openqa.selenium.interactions.Action;
org.openqa.selenium.interactions.Actions;
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);
In this example, the sequence of steps to perform multiple actions are:
To perform these actions, the Action class has methods such as:
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);
action.sendKeys(“TexttoBeConvertAndSendInUpperCase);
actions.keyUp(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
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.
The last step in executing the Action class is performing the actions sequence through the perform() method of the Action interface. The syntax is:
action.perform();
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.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources