What is Actions class in Selenium?
Actions in Selenium is a class that allows us to make a sequence of actions that we would like to perform. For example, Press shift button, type something and then release.
In simple words, actions class in selenium are used to perform multiple actions at the same time. For example, double-click, right-click, moving a mouse, drag & drop, etc. These kinds of functionalities in the web application can be done by a special API called Actions API. By using actions API, we can perform either one action at a time or we can perform by queuing up actions one after another.
Actions class is present in org.openqa.selenium.interactions.Actions package. It extends super class object.
public class Actions
The actions class also implements the builder patterns by creating a composite action that contains a group of actions specified by method calls.
CompositeAction in selenium is a class that collects all the actions and performs them at the same time. It implements an Action interface.
What is Action in Selenium?
Action in Selenium is an interface that provides us two methods: perform() and build(). These two methods of action interface are implemented by the actions class. Let’s see the build() and perform() methods in detail.
It does not accept anything as a parameter but returns a composite action.
Action action = actions.build();
Since the return type of build() method is action, we will store it by using a variable action of type Action. In the above syntax, actions is a reference variable of the object of actions class.
How to create an Object of Actions class in Selenium?
Since the actions is an ordinary class, its object is created by using the new keyword. After creating object, we will have to pass the WebDriver object reference as a parameter to its constructor.
The syntax to create an object of actions class is as follows:
Actions actions = new Actions( WebDriver driver);
Note: In the object creation of select class, we pass WebElement as a parameter to its constructor. But in the object creation of actions class, we pass WebDriver object to its constructor.
When you will create an object of the actions class and pass the WebDriver object as a parameter to its constructor, you can access all the methods provided by the Actions class by just typing actions + dot.
The actions + dot (actions.) will provide the list of all methods under the Actions class. You can choose any method according to the requirement of your test case.
Let’s see a glance at the following source code to look at the list of all methods under the actions class.
Program source code 1:
A list of all methods under the actions class can be seen in the below screenshot.
Now let’s move towards to learn about the various types of methods under this Actions class.
Actions class Methods in Selenium
Actions class provides various methods that return an actions object unless specified explicitly. Basically, all the methods of actions class can be divided into two categories: Mouse based actions methods and Keyboard-based actions methods. So, let us see the first mouse-based actions method.
Mouse based Actions Methods:
The click() method does not take any parameter. It returns a self-reference.
The syntax for the click method in combination with another method to make composite action is as follows:
2. click(WebElement )
This method is used to left mouse click directly in the middle of specified web element.
This method takes an input parameter as an instance (variable) of WebElement on which click action has to be performed. It returns an Actions instance.
The above syntax is equivalent to actions.moveToElement(element).click();.
It does not take any input parameters and returns an actions class instance.
In the preceding syntax, the moveToElement(element) method has been used to move mouse cursor to the location of element and double-clicked on the element.
4. doubleClick(WebElement element)
This method accepts an input parameter as target web element that has to be double-clicked. It returns an actions class instance.
The preceding method does not accept any input parameter and returns Actions instance.
In the preceding syntax, the cursor will first move to the specified location of web element and then right-click on it.
In this example, the cursor will move to the location of element “two” and then perform right-click operation.
6. contextClick(WebElement element)
It takes an input parameter as an instance of WebElement that has to be right-clicked and returns an actions class.
This method accepts an input parameter as the target web element where the mouse cursor has to be moved.
8. moveToElement(WebElement, int, int )
This method is used to move the mouse cursor from the top-left corner of the web element to an offset position on the web page. The element is scrolled into view and its location is calculated using getBoundingClientRect.
In the above code, xOffset is an input parameter that represents the amount of offset to be moved along the x-axis. If x is a positive value, the mouse cursor will move to the right. The negative value of x is used to move the mouse cursor to the left.
yOffset is an input parameter that represents the amount of offset to be moved along the y-axis. The positive value of y moves the mouse cursor down along the y-axis whereas, the negative value of y moves the cursor towards the top along the y-axis.
If the xOffset and yOffset values are provided such that they move the cursor out of the document, MoveTargetOutOfBounds exception is thrown.
actions.moveToElement(element, 524, 280).build().perform();
9. moveByOffset(int, int)
In the above syntax, the mouse cursor will click at the current location, move to the location (300, 200) by holding it.
The preceding method takes an input parameter as a WebElement that has to be clicked and held. It returns actions class instance. This method is equivalent to actions.moveToElement(element).clickAndHold();.
In the above syntax, we have provided a web element to the clickAndHold() method that will be clicked and move to the location by holding it.
The above method does accept any input parameter and returns the actions class instance.
You will get undefined behavior if you attempt to call release() method without calling clickAndHold() method first.
This method takes an input parameter as the target web element where the held web element has to be dropped. The return type of this method is an instance of actions class.
14. dragAndDrop(WebElement, WebElement)
The dragAndDrop() method takes two input parameters as source element and target element. The return type of this method is the actions class.
We can call dragAndDrop() method in the following way:
In the preceding syntax, the source and target web elements are identified by their IDs.
15. dragAndDropBy(WebElement, int, int)
The dragAndDropBy() method is used to drag a web element on to the location of given offset and drop it.
This method takes three input parameters as target WebElement to be dragged, xOffset parameter, and yOffset parameter.
actions.dragAndDropBy(dragMe, 300, 200 ).perform();
In the above syntax, dragMe is a web element that is identified by its id and the element dragMe is dragged 300 horizontally and 200 vertically.
Keyboard based Actions Methods:
There are also two overloaded methods of keyDown() and keyUp(). They are keyDown(WebElement element, Keys key)
keyUp(WebElement element, Keys key).
Both methods are used to execute an action directly on the web element.
The sendKeys() method is used to type in alphanumeric and special character keys into the WebElement such as textarea, textbox, and so on. The syntax of sendKeys() method is as follows:
The overloaded method of sendKeys() method is sendKeys(WebElement target, CharSequence keyToSend). It is equivalent to calling actions.click(element).sendKeys(keysToSend).
Difference between Action and Actions class in Selenium
There are the following differences between action and actions class in selenium. They are as follows:
1. Action in selenium is an interface whereas, actions in selenium is a class.
2. Action provides two methods such as build() and perform() whereas, actions class provides various methods to perform complex mouse actions and keyboard shortcuts.
3. Actions class extends object class and implements an action interface.
Hope that this tutorial has covered almost all the important points related to Actions class in Selenium WebDriver with examples. In the next tutorial, we will learn some advanced ways of performing complex mouse actions on WebElement such as moving the mouse, double-clicking, dragging and dropping using actions API of Selenium.
Thanks for reading!
⏪ PrevNext ⏩