Selenium Actions Class: A Comprehensive Guide

ยท

3 min read

Introduction:

Selenium, the go-to tool for web automation, provides a plethora of features to create robust and comprehensive test suites. ๐ŸŒ One key player in the Selenium toolkit is the Actions class, a versatile tool for simulating complex user interactions. In this blog post, we will unravel the mysteries of the Actions class ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ”

Understanding the Actions Class:

What is the Actions Class?

The Actions class in Selenium is a powerful component that allows testers to simulate a series of actions, combining keyboard and mouse events seamlessly. This class resides in the org.openqa.selenium.interactions package and is instrumental in crafting intricate test scenarios. ๐Ÿ› ๏ธ๐Ÿงช

Building and Performing Actions:

  1. 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);
    
  2. 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:

    1. 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 the perform() method.

      Here's an example:

       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();
      

      In this example, the build() method is used to compile the moveToElement, click, and sendKeys actions into a single composite action, and then perform() is called to execute that composite action.

    2. 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.

      Here's an example:

       Actions actions = new Actions(driver);
      
       // Define a sequence of actions 
       actions.moveToElement(element1).click().sendKeys("Hello");
      
       // Perform the actions 
       actions.perform();
      

      In this example, the perform() method is called to execute the sequence of actions, which includes moving to an element, clicking, and sending keys.

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:

  1. keyDown(modifier key): Performs a modifier key press. For example:

     actions.keyDown(Keys.SHIFT).sendKeys("a").keyUp(Keys.SHIFT).perform();
    
  2. sendKeys(keys to send): Sends keys to the active web element.

     actions.sendKeys("Hello, Selenium!").perform();
    
  3. keyUp(modifier key): Performs a modifier key release.

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

Mouse Events:

  1. click(): Clicks at the current mouse location.

     actions.click().perform();
    
  2. doubleClick(): Performs a double-click at the current mouse location.

     actions.doubleClick().perform();
    
  3. contextClick(): Performs a context-click at the middle of the given element.

     actions.contextClick(element).perform();
    
  4. clickAndHold(): Clicks (without releasing) in the middle of the given element.

     actions.clickAndHold(element).moveToElement(anotherElement).release().perform();
    
  5. dragAndDrop(source, target): Click-and-hold at the location of the source element, moves to the location of the target element.

     actions.dragAndDrop(sourceElement, targetElement).perform();
    
  6. 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();
    
  7. moveByOffset(x-offset, y-offset): Moves the mouse from its current position (or 0,0) by the given offset.

     actions.moveByOffset(50, 20).perform();
    
  8. moveToElement(toElement): Moves the mouse to the middle of the element.

     actions.moveToElement(element).perform();
    
  9. release(): Releases the depressed left mouse button at the current mouse location.

     actions.release().perform();
    
ย