How to Handle Actions Class in Selenium?
Automation • Selenium Testing •
79826 Views | 9 Min Read
Selenium has been widely used in automating web applications as it stands out from other testing tools by providing numerous ways to test those applications. No matter how complex the UI is, Selenium provides multiple options to automate it thereby reducing or completely wiping away the manual effort. When it comes to testing Windows-based applications or any standalone applications, we have different tools like QTP/UFT which perform user actions in the window handles.
How do we automate the web applications when there is a need to interact with the browser through a mouse or keyboard?
Obviously, the answer is ‘Selenium’. We can use it for performing such interactions with the browser through our automation script.
How do we interact with the browser using Selenium test automation?
The answer is ‘Actions Class’. Yes, the Actions class in Selenium provides multiple methods to perform a single action or series of actions in the browser. In this guide about ‘what is actions class in Selenium’, I will take you through the Actions class and its implementations. Before we dive deep into this concept, let us understand the basics.
What Is Actions Class In Selenium?
Actions like clicking a button, entering a keyword in the search bar are prime examples of how we use a mouse or keyboard. These interactions are done through the mouse and the keyboard can be automated by using the Actions class in Selenium. As the name suggests, the Actions class contains a collection of actions that can be performed in a web application.
It is quite easy to understand what is Actions class in Selenium, the tricky part is implementing it. The actions that can be performed in a browser are broadly classified into two categories namely-
- Mouse actions
- Keyboard actions
The various mouse actions that are provided by the Actions class are-
- click() – clicks at the current location
- doubleClick() – performs a Double click at the current mouse location
- contextClick() – performs a Right click at the current mouse location
- dragAndDrop(WebElement source, WebElement target) – drags an element from the source location and drops in target location
- moveToElement(WebElement target) – moves to the target element
The various keyboard actions that are provided by the Actions class are-
- keyUp(WebElement target, java.lang.CharSequence key) – performs a key release after focusing on the target element
- keyDown(WebElement target, java.lang.CharSequence key) – performs a key press after focusing on the target element
- sendKeys(WebElement target, java.lang.CharSequence… keys) – types the value.
Apart from the above methods, there are many other methods which can be used based on our requirements. The extensibility of actions that can be performed is one of the best features of the Actions class in Selenium.
Do you know how to test PDF Files using Selenium test automation?
How To Implement The Actions Class?
Now that you understand what is Actions class in Selenium, it is time to start implementing this class in your Selenium test automation scripts. To implement the Actions class in Selenium automation script, follow the steps given below-
Step 1: First, we have to import the package
Step 2: To use the methods provided by the Actions class, we need to create an object of this class and pass the WebDriver as an argument.
// instantiate the WebDriver
WebDriver driver = new ChromeDriver();
// create an object of the Actions class
Actions act = new Actions(driver);
Step 3: The object created can now be used to perform any actions. You can see various actions that are provided by this class once you create an object.
Now, let us see the implementation of each method in detail with an example.
Using the Actions class in Selenium, we can implement the sendKeys() method to type specific values in the application.
Below is a simple code which is used to search a product in a search engine by passing the product’s name in the search box-
That is how you use the actions class in Selenium with sendKeys() method. You must be wondering- why do we add build()and perform() at the end?
The build() method generates a composite action containing all actions which are ready to be performed. The perform() method is used to perform the series of actions that are defined.
2. Mouse click
Actions class in Selenium can also perform a mouse click either on a specific element or at the current mouse location.
Below is a simple code which is used to search a product in a shopping website and click the search icon-
3. ContextClick & doubleClick
In case there is a requirement to click a button twice or right click, Actions class in Selenium is capable of doing that as well. We can use the doubleClick() and contextClick() methods respectively.
Below is a simple code which can be used to perform both these actions-
This method is used to move to a specific target element on the web page. Sometimes there may be some sub options or submenus which would be visible only when the mouse cursor is moved or hovered over the main menu. In such cases, Actions class in Selenium provides the moveToElement() method. We can also provide the x coordinate and the y coordinate as parameters in this method in addition to the target element.
Let us try to automate the scenario below-
Navigate to https://www.lambdatest.com/ website. Hover over the Resources tab with your mouse and click the Blog sub menu to read the articles posted in the blog.
The dragAndDrop(WebElement source,WebElement target) method is used to drag an element from source and drop it into the target location. There are two ways to perform this with the help of Actions class in Selenium-
- Use the dragAndDrop(WebElement source,WebElement target) method.
- Use the below series of actions.
clickAndHold(WebElement source) ? moveToElement(WebElement target) ? release
This is the way we manually drag and drop a file or an image from a source to destination-
The sendKeys() method that we have seen earlier was primarily used to send the text. In this section, we shall see how we can use Actions class in Selenium to send other Keys like CTRL, ALT, SHIFT etc.
While we are searching for some products on a shopping website, we would type the ‘Product name’ and press Enter from the keyboard. This action would be the same as the one performed by clicking the Search button.
Below is the code for performing a search operation only using the keyboard actions class in Selenium-
7. KeyUp / KeyDown
The keyUp and keyDown methods are used for imitating the keyboard actions of pressing and releasing the keys. These methods serve multiple methods like converting the texts into uppercase or lowercase, copy text from source and paste in destination location, scrolling up and down the web page, multiple value selection etc. This is one of the most repeatedly used Actions class in Selenium test automation.
Let us see sample code for each case-
a. Converting Texts to Uppercase
When we want to enter the text manually, we press down the SHIFT key and enter the text simultaneously without releasing the SHIFT key. We can apply the same logic to our code like below-
When the above code is executed, the result would be as seen in the image below.
b. Scroll Up & Down the page
You can also scroll to the top or the bottom of a page using the Actions class in Selenium test automation.
Note: I have intentionally added some wait time in the above code so that you can see how the page scrolls down and again to the top of the page.
c. Copy & Paste
Copying some text from the source and pasting them in a target location can also be done with the help of actions class in Selenium test automation. The logic is the same as how we copy and paste texts manually.
Below is the sample code for copying the text and pasting it in another location-
d. Refresh the page
Actions class in Selenium test automation can also be used to perform the basic operation of refreshing. This might not seem as useful but it does have its perks when a browser is not able to read the contents of a page in the first go.
Below is a sample code that can be used to refresh a web page-
To sum it up, we have seen what is Actions class in Selenium and the different methods which are used to interact with a browser. We have seen examples for performing different actions like clicking the mouse, typing text, dragging and dropping, moving to a specific element, double click, right-click, copy and paste content, refreshing a page, converting text to uppercase or lowercase among other things. I hope you understand the importance of the Actions class in Selenium and how it could be used in different contexts.
To make your Selenium test automation scripts more effective, I would recommend you to use a cloud Selenium grid and speed up your release cycles. LambdaTest provides a cloud-based Selenium grid with the option to perform cross browser testing on over 2000+ OS and browsers. Try to implement these automation scripts effectively on the LambdaTest platform and let me know your feedback. Feel free to share any other scenarios where you have implemented the Actions class in Selenium effectively. Do share this with your friends and colleagues so that it would also help me in gaining knowledge on this topic.
Stay tuned for more interesting blogs on Selenium test automation. Happy Testing!
7602 Views | 7 Min Min Read
55092 Views | 8 Min Min Read
78374 Views | 8 Min Min Read
106437 Views | 19 Min Min Read