top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Actions Class in Selenium


As we all know, in today’s world; Selenium is widely used for automating web applications. All the web applications need to use mouse or keyboard to interact with browser for creating reliable and detailed automated tests that simulate real world user interactions. In Selenium we can achieve the automation of such complex user interactions with the help of Actions class. 


What is Actions class? 

 

  • Actions class is a predefined class in Selenium WebDriver library which helps to handle complex user gestures and interactions such as mouse movements (double clicks, right clicks drag and drop actions etc.) and keyboard inputs. 

  • It helps to automate various keyboard and mouse events beyond simple clicks and inputs 

  • It is a collection of multiple Actions needed to perform these operations 

 


A general code used to define and invoke Actions class has the following syntax: 

Actions actions = new Actions(driver); actions.moveToElement(element) 

 .click()  

.sendKeys("text") 

 .build()  

.perform(); 


There are mainly two types of actions performed on any web browser: 

 

Mouse Actions 

Keyboard Actions 


Let's try to understand the most commonly used methods from these categories


 Mouse actions: 

  • click()      - clicks on an element 

  • doubleClick() -   double clicks on an element 

  • contextClick() -    right clicks on an element 

  • MovetoElement(WebElement target) -     moves the mouse pointer to the target element/hover over 

  • DragAndDrop(WebElement source, WebElement target) - drags and drops the element from source to target 

 

  Keyboard actions :  

  • sendKeys(sendKeys(WebElement target, java.lang.CharSequence… keys) - Sends a series of keys to the element 

  • keyUp(WebElement target, java.lang.CharSequence key) - Performs key release after focusing on the target 

  • keyDown(WebElement target, java.lang.CharSequence key)  - Performs key press without release.

 

Apart from the above mentioned methods there are several more methods which can be used based on our requirements. 

 

 

Implementation of Actions Class 

 

The following steps need to be performed to implement Actions class 


  1. Import the package


import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.interactions.Actions; 

 



2.Create an instance of the Actions class and pass WebDriver as an argument 

WebDriver driver = new ChromeDriver();  

Actions actions = new Actions(driver); 


 3.Using this object now we can build a sequence of actions.  

WebElement element = driver.findElement(By.id("elementId”)); actions.moveToElement(element) .click() .sendKeys("Hello") .build();



We can see different actions provided by this class once we create the instance ;as seen in the above image.

 

4. Perform the action sequence :


 

actions.click(element).build().perform(); 

For each method discussed above ; let us see how to implement Actions class : 

  1. Click an element

actions.click(element).perform();



2. Double click an element

actions.doubleClick(element).perform(); 



3. Right click an element/ Context click

actions.contextClick(element).perform(); 


4.  Hover over an element

actions.moveToElement(element).perform();


5. Drag and drop

WebElement source = driver.findElement(By.id("sourceElement"));  

WebElement target = driver.findElement(By.id("targetElement")); actions.dragAndDrop(source, target).perform(); 


6. sendKeys() method - This method is used to simulate typing into a text area 

WebElement searchBox = driver.findElement(By.id("search"));  

Actions actions = new Actions(driver); 

 actions.sendKeys(searchBox, "Selenium testing").perform();

sendKeys() method is also used to send special key combinations  :

WebElement inputField = driver.findElement(By.id("input")); 

inputField.sendKeys(Keys.CONTROL + "a");  


7. keyDown() method- This method is used to press and hold down a key on the keyboard 

actions.keyDown(Keys.SHIFT).perform(); 


8. keyUp() method- This method is used to release a key that was pressed  down previously. 

actions.keyUp(Keys.SHIFT).perform(); 


We can also use combination of keyboard actions for situations like keyboard shortcuts:

Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

Another example of using keyboard combination for copy-paste of text : 

WebElement sourceElement = driver.findElement(By.id("source")); 

 WebElement targetElement = driver.findElement(By.id("target"));  

Actions actions = new Actions(driver);  

 

actions.click(sourceElement) 

 .keyDown(Keys.CONTROL)  

.sendKeys("A")  

.keyUp(Keys.CONTROL)  

.keyDown(Keys.CONTROL)  

.sendKeys("C")  

.keyUp(Keys.CONTROL) 

 .click(targetElement) 

 .keyDown(Keys.CONTROL)  

.sendKeys("V") 

.keyUp(Keys.CONTROL) 

 .perform(); 

 

Here the sequence of actions would be : 

  • Click the source webelement 

  • Select all Text( Ctrl+A) 

  • Copy the text (Ctrl+C) 

  • Click the target webelement 

  • Paste the text(Ctrl+V) 


9. Scroll to a particular element

 WebElement element = driver.findElement(By.id("elementId"));  

 Actions actions = new Actions(driver); actions.moveToElement(element).perform(); 

 


Some advanced use cases of Actions Class

There are some advanced use cases where Actions class can be very useful.Lets discuss a few of those here :


  • Complex interactions which involves interacting with multiple elements in a webpage and performing multiple actions in a sequence  

     

actions.moveToElement(element1)  

.pause(1000) 

actions.moveToElement(element2) 

 .click()  

.pause(500) 

.clickAndHold(draggableElement) .moveToElement(droppableElement)

.release() 

.moveToElement(element3) . 

.click() 

.sendKeys(“text”) 

.click() 

.build()  

.perform(); 

  • Drag and drop to a particular offset – This is also an advanced use case where instead of dragging one element to another we may have to drag a Webelement to a specific position on the screen


  

WebElement element = driver.findElement(By.id("zoomableElement")); 

 Actions actions = new Actions(driver); 

actions.moveToElement(element) 

       .clickAndHold() 

       .moveByOffset(-50, -50)

       .moveByOffset(50, 50)

       .release() 

       .perform(); 

 

  • Slider Control – By clicking and dragging slider handle to a particular position we can control sliders. This is useful for testing scenarios like setting ranges or adjusting filters. 

     

WebElement slider = driver.findElement(By.id("slider")); 

Actions actions = new Actions(driver); 

actions.clickAndHold(slider) 

       .moveByOffset(50, 0)  

       .release() 

       .build() 

       .perform(); 

 

 


Best practices while using Actions class



To ensure that our test cases are reliable, maintainable and efficient we need to follow certain best practices while using Actions class :

 

1.Use of Explicit Waits  :  Always wait for elements to be loaded and get interactable before performing any actions on them. Use WebDriverWait with appropriate ExpectedConditions. 

 

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 

WebElement  element =wait.until(ExpectedConditions.elementToBeClickable(By.id("element"))); 

Actions actions = new Actions(driver); 

actions.moveToElement(element).click().perform(); 

 

 

2. Chain Actions together :  As we have already discussed earlier; we can combine multiple actions into a single chain for  complex keyboard and mouse interactions. But we should also keep in mind that too much chaining may cause the code to be harder to debug ; so use sensibly. 


3.Use build() and perform() :  Always call build() to create action sequence and perform() to execute the action. But also keep in mind to call perform() method only once in an action sequence to avoid unexpected behaviour. 

 

4. Handle StaleElementReferenceExceptions:  Implement retry logic for actions that may fail due to state changes of element 


 public void performActionWithRetry(WebElement element, Consumer<WebElement> action, int maxRetries)  

{  

int attempts = 0;  

while (attempts < maxRetries) {  

try {  

action.accept(element);  

return; }  

catch (StaleElementReferenceException e)  

 element = driver.findElement(By.id("elementId"));        // Re-locate the element }  

attempts++;  

}  

throw new RuntimeException("Action failed after " + maxRetries + " attempts");  

 

 

5.Use appropriate pauses between actions to allow time for the page to respond.  

   actions.moveToElement(element).pause(Duration.ofMillis(500)).click().perform(); 

       

6. Combine with JavaScript execution for tricky scenarios and while involving elements that are difficult to interact using standard Selenium methods. 

 

7.Make sure to test on different browsers as actions may behave differently across browsers. 


Benefits of Actions Class


Now that we have understood that the Actions class in Selenium provides a powerful set of methods to handle complex user interactions involving keyboard and mouse; lets summarize a few of its benefits :



  • Advanced interaction simulation: Allows testers to automate sophisticated mouse and keyboard actions like hover, drag-and-drop, and multi-key inputs that closely mimic real user behavior. 

  • Improved test coverage: Enables testing of complex UI elements and interactions that cannot be handled by basic WebDriver commands.

  • Enhanced precision: Provides fine-grained control over mouse movements and keyboard inputs for more accurate testing. 

  • Cross-browser compatibility: Offers a consistent API for performing actions across different browsers. 

  • Chaining of actions: Allows multiple actions to be combined into a single fluent sequence, simplifying test scripts. 

  • Handling of dynamic elements: Useful for interacting with elements that only appear on hover or other triggers. 

  • Flexibility: Supports a wide range of interactions from simple clicks to complex drag-and-drop operations. 

  • Integration with waits: Can be combined with explicit waits for improved synchronization and reliability.

  • Better handling of AJAX and dynamic content: Allows for more effective testing of modern web applications with dynamic updates. 


By leveraging the Actions class, testers can create more comprehensive, realistic, and robust automated tests, leading to improved software quality and user experience. Its ability to handle complex scenarios makes it an essential tool in any Selenium tester's toolkit.  


17 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page