Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Selenium is an open-source tool that helps you automate testing web applications. You can write scripts to mimic user interactions with your website. Action class is a utility class provided by the Selenium WebDriver.
This article will teach you about the action class in Selenium with the help of examples.
Before getting started, you should go through this article to get familiar with Selenium.
What is the Action Class in Selenium?
The Actions class in Selenium is a part of the WebDriver API that facilitates performing complex user interactions, such as mouse and keyboard actions, in web applications. It allows the automation of tasks like hovering over elements, dragging and dropping, and handling keyboard events. The Actions class is particularly useful for simulating real-world user behavior in test automation scripts.
Basics of Action Class in Selenium
The action class handles keyboard and mouse events by using the advanced user interaction API. It can be used to create a complex series of operations/actions to be performed on a web page.
It supports the following actions:-
Double-clicking
Right-clicking
Drag and drop
Opening drop-down boxes, etc.
The following section will introduce you to some methods present in the action class in selenium.
Methods in Action Class
The action Class in Selenium handles both mouse and keyboard actions.
Mouse Action Methods
The methods allow you to mimic mouse interactions such as clicking, dragging, etc.
Following are some commonly used mouse action methods:-
Method
Description
doubleClick()
Performs double-click on the specified element.
contextClick()
Performs right-click on the specified element.
clickAndHold()
Clicks on the specified element without releasing.
dragAndDrop()
Clicks on the source, drag, and release on the target.
moveToElement()
Moves the cursor to the specified element.
Keyboard Action Methods
The methods allow you to mimic keyword interactions such as pressing keys, adding information to text fields, etc.
Following are some commonly used keyboard action methods:-
Method
Description
sendKeys()
Sends a sequence of keystrokes to the specified element.
keyUp()
Releases the specified modifier key.
keyDown()
Presses the specified modifier key.
These methods are used for automating the testing process of web applications.
In the next section, you will learn more about these methods with the help of examples.
How to Use Action Class in Selenium?
To use the Action class in Selenium you need to follow these steps:
Instantiate Actions Class: You can create an object of the Actions class in Selenium.
Perform Mouse Actions: Now, you can use methods like moveToElement, click, doubleClick, contextClick for mouse interactions.
Perform Keyboard Actions: Then if you want to use keyboard actions then sendKeys method is used to simulate keyboard actions.
Chaining Actions: You can also chain multiple actions together using the build() and perform() methods.
Handling Drag-and-Drop: Then you can use dragAndDrop or dragAndDropBy methods for drag-and-drop operations.
Handling Key Events: You can also utilize methods like keyDown, keyUp for handling key events.
Handling Composite Actions: You can use Action interface for more complex sequences of actions.
Release Resources: Always release resources by calling release() or perform() at the end of the sequence.
Handling Advanced Scenarios: For more advanced scenarios, consider using the Actions class for handling complex interactions.
Verify Results: After performing actions, verify the expected results in the application.
Implementation of Actions class method
Here are the steps to implement the Actions class methods in Selenium:
1. Import Actions Class: Import the Actions class from the org.openqa.selenium.interactions package.
import org.openqa.selenium.interactions.Actions;
2. Create WebDriver Object: Instantiate the WebDriver (e.g., ChromeDriver) and open the desired web page.
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
3. Instantiate Actions Class: Create an object of the Actions class by passing the WebDriver instance.
Actions actions = new Actions(driver);
4. Perform Mouse Actions: Use methods like moveToElement, click, doubleClick, contextClick to perform mouse actions.
WebElement element = driver.findElement(By.id("exampleElement"));
actions.moveToElement(element).click().build().perform();
5. Perform Keyboard Actions: Utilize sendKeys method for keyboard actions.
10. Release Resources: Always release resources by calling release() or perform() at the end of the sequence.
actions.release();
11. Close the Browser: Close the WebDriver instance to end the test.
driver.quit();
Examples of Action Class in Selenium
1. Perform Click Action on the Web Element
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class ClickExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement buttonElement = driver.findElement(By.id("exampleButton"));
Actions actions = new Actions(driver);
actions.click(buttonElement).build().perform();
driver.quit();
}
}
In this example, this code opens a web page and locates a button element using its ID. It creates an Actions object and performs a click action on the button using the click method. The build() and perform() methods are used to execute the sequence of actions. Finally, the WebDriver is closed.
2. Perform Mouse Hover Action on the Web Element
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class HoverExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement hoverElement = driver.findElement(By.id("exampleHover"));
Actions actions = new Actions(driver);
actions.moveToElement(hoverElement).build().perform();
driver.quit();
}
}
In this example, this code hovers the mouse over a designated element on a web page. The moveToElement method is used to perform a mouse hover action. The build() and perform() methods are employed to execute the action sequence. The WebDriver is closed at the end.
3. Perform Double Click Action on the Web Element
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DoubleClickExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement doubleClickElement = driver.findElement(By.id("exampleDoubleClick"));
Actions actions = new Actions(driver);
actions.doubleClick(doubleClickElement).build().perform();
driver.quit();
}
}
In this example, this code performs a double-click action on a specified element. The doubleClick method is used to trigger the double-click action. The build() and perform() methods ensure the execution of the action sequence. Finally, the WebDriver is closed.
Advantages of Actions Class in Selenium
The following are some advantages of the actions class:-
Actions can be chained to perform complex series of user interactions which allows you to test your web applications rigorously with realistic scenarios.
Actions are supported by most modern browsers such as Chrome, Safari, Firefox, etc.
Web components can be easily selected by methods similar to the JavaScript DOM methods, such as SelectById().
It can handle complex modern UI elements such as slides and dropdown menus.
Limitations of Actions Class in Selenium
The actions class allows you to automatize complex user interactions. However, it still has some limitations:-
Some browsers may not be compatible with the actions class in Selenium. Actions such as drag and drop, right-clicking, etc., may not work as expected.
Certain components on your websites may not respond to the actions performed by the driver, e.g., custom forms, complex JavaScript-driven elements, etc.
The actions class doesn’t support interactions for mobile browsers, frameworks such as Appium are recommended for such cases.
Actions cannot wait for asynchronous functions to finish. This results in synchronization issues between the script and the web application as the to complete cannot be precomputed for async functions.
Selenium WebDriver is a powerful tool in the Selenium suite that allows users to automate tests for web applications. It works by interacting directly with the web browser using browser-specific drivers.
What is build in actions class in selenium?
The Actions class in Selenium provides a way to perform complex user interactions, such as mouse and keyboard actions, enabling more realistic testing scenarios.
What is the difference between robot class and action class in selenium?
The Robot class operates at the system level, simulating user input, while the Actions class is specific to Selenium, providing a higher-level API for performing interactions on web elements.
How can I chain multiple actions?
Actions can be chained one after another by calling the methods one after another. For example, action.click().doubleClick().
Conclusion
The Actions class in Selenium proves to be a powerful tool for handling complex user interactions in web applications. By offering a versatile set of methods to simulate mouse and keyboard actions, it enhances the capabilities of Selenium, facilitating more realistic and effective testing scenarios.
Hope this article helped you understand the Action Class in Selenium. If you're interested, you can explore the courses we offer.