Actions Class in Selenium ๐๐ก
Selenium WebDriver provides a powerful toolset for automating web applications, and the Actions
class is a key component for handling complex user interactions like mouse movements, keyboard actions, and more. In this comprehensive guide, we'll dive into the world of the Actions
class in Selenium with Java, exploring its capabilities and providing real-world examples.
Understanding the Actions Class ๐ญ
What is Actions Class?
The Actions
class in Selenium enables the automation of complex user interactions that involve mouse and keyboard actions. It provides a way to chain multiple actions together, creating sequences of events that mimic user behaviour.
Building and Performing Actions:
Instantiating the Actions Class: To use the Actions class, start by creating an instance associated with a WebDriver instance.
WebDriver driver = new ChromeDriver(); Actions actions = new Actions(driver);
Building and Performing: In Selenium WebDriver, the Actions class provides a way to automate complex user interactions, such as mouse movements, keyboard inputs, and more. The build() and perform() methods in the Actions class are used to execute a series of actions that have been defined. Let's break down what each of these methods does:
build()
Method:The
build()
method is used to compile all the actions into a single composite action. It is optional and is typically used when you want to perform a sequence of actions as a single action. This method is often used in conjunction with theperform()
) method.Actions actions = new Actions(driver); // Define a sequence of actions actions.moveToElement(element1).click().sendKeys("Hello"); // Build the composite action Action compositeAction = actions.build(); // Perform the composite action compositeAction.perform();
perform()
Method:The
perform()
method is used to execute the actions that have been defined using the Actions class. It is the method that triggers the actual interaction with the browser based on the sequence of actions you've specified.Actions actions = new Actions(driver); // Define a sequence of actions actions.moveToElement(element1).click().sendKeys("Hello"); // Perform the actions actions.perform();
In summary, build() is used to compile a sequence of actions into a single composite action, and perform() is used to execute the actions. In many cases, you might not explicitly use build() unless you need to create a composite action, and you can directly chain the actions and call perform() to execute them.
Methods in Actions Class:
Keyboard Events:
keyDown(modifier key)
: Performs a modifier key press. For example:
actions.keyDown(Keys.SHIFT).sendKeys("a").keyUp(Keys.SHIFT).perform();
sendKeys(keys to send)
: Sends keys to the active web element.
actions.sendKeys("Hello, Selenium!").perform();
keyUp(modifier key)
: Performs a modifier key release.
actions.keyUp(Keys.SHIFT).perform();
Mouse Events:
click()
: Clicks at the current mouse location.
actions.click().perform();
doubleClick()
: Performs a double-click at the current mouse location.
actions.doubleClick().perform();
contextClick()
: Performs right-click on the mouse.
actions.contextClick(element).perform();
clickAndHold()
: Clicks (without releasing) in the middle of the given element.
actions.clickAndHold(element).moveToElement(anotherElement).release().perform();
dragAndDrop(source, target)
: Click-and-hold at the location of the source element, moves to the location of the target element. Drags the element from one point and drops to another.
actions.dragAndDrop(sourceElement, targetElement).perform();
dragAndDropBy(source, xOffset, yOffset)
: Click-and-hold at the location of the source element, moves by a given offset.
actions.dragAndDropBy(sourceElement, xOffset, yOffset).perform();
moveByOffset(x-offset, y-offset)
: Moves the mouse from its current position (or 0,0) by the given offset.
actions.moveByOffset(50, 20).perform();
moveToElement(toElement)
: Moves the mouse to the middle of the element.
actions.moveToElement(element).perform();
release()
: Releases the depressed left mouse button at the current mouse location.
actions.release().perform();
Conclusion:
Mastering the Actions class in Selenium opens up a world of possibilities for crafting sophisticated test scenarios. Whether you're simulating keyboard events or orchestrating intricate mouse interactions, the Actions class provides the tools needed for comprehensive web automation testing. By understanding its methods and leveraging the build and perform paradigm, testers can create resilient and effective automated tests that mimic real user behaviour on the web. ๐โจ Happy Testing! ๐งช๐