Home Guide How to handle Action class in Selenium

How to handle Action class in Selenium

By Neha Vaidya, Community Contributor -

To test an application, one needs to perform a number of user actions on it. To perform any operations on the web application such as double-click, selecting drop-down boxes, etc. the actions class is required. This article discusses how to handle the action class in Selenium.

What is Action Class in Selenium?

Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop, clicking on multiple elements with the control key, among others. These operations are performed using the advanced user interactions API. It mainly consists of Actions that are needed while performing these operations.

Action class is defined and invoked using the following syntax:

Actions action = new Actions(driver);

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

Methods of Action Class

Action class is useful mainly for mouse and keyboard actions. In order to perform such actions, Selenium provides various methods.

 Mouse Actions:

  1. doubleClick(): Performs double click on the element
  2. clickAndHold(): Performs long click on the mouse without releasing it
  3. dragAndDrop(): Drags the element from one point and drops to another
  4. moveToElement(): Shifts the mouse pointer to the center of the element
  5. contextClick(): Performs right-click on the mouse

Keyboard Actions:

  1. sendKeys(): Sends a series of keys to the element
  2. keyUp(): Performs key release
  3. keyDown(): Performs keypress without release

Now, let’s understand how to perform various mouse and keyboard actions.

Learn the fundamentals of Selenium coding with the help of Selenium Commands.

Examples of Action Class in Selenium

  • Perform Click Action on the Web Element

Test Scenario: Visit the Browserstack home page and click on the Get Started Free button.

BrowserStack Home Page

Code Snippet:

driver.get("https://www.browserstack.com/");
Actions action = new Actions(driver); 
element = driver.findElement(By.linkText("Get started free"));

action.moveToElement(element).click();

//using click action method
  • Perform Mouse Hover Action on the Web Element

Test Scenario: Perform Mouse Hover on Live Tab and App Automate Tab on the Browserstack Website.

BrowserStack Products

Code Snippet:

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

public class Mouse {

   public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");

         WebDriver driver = new ChromeDriver();

       driver.manage().window().maximize();

       driver.get("https://www.browserstack.com/");

       ((JavascriptExecutor) driver).executeScript("scroll(0,300)");

       Actions ac = new Actions(driver);

WebElement live= driver.findElement(By. cssSelector("div.product-cards-wrapper--click a[title='Live']"));     
ac.moveToElement(live).build().perform();

Thread.sleep(3000);

WebElement automate= driver.findElement(By.cssSelector("div.product-cards-wrapper--click a[title='App Automate']"));  automate.click();

Thread.sleep(2000);

//Thread.sleep(4000);

driver.quit();  

   }

}

In the code above, Selenium will perform the mouse hover action on the Live tab and then move to the App Automate tab and click.

  • Perform Double Click Action on the Web Element

Test Scenario: Perform Double Click Action on Free Trial Button in the Browserstack Home page.

Click on Free Trial - Action Class in Selenium

Code Snippet:

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 Mouse {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.browserstack.com/");

Actions a = new Actions(driver);


//Double click on element

WebElement trialaction = driver.findElement(By.xpath("//a[@id='free-trial-link-anchor']"));

a.doubleClick(trialaction).perform();

   }

}

In the code above, Actions class is created to perform the double click action on the element named Free Trial.

Learn how to perform various actions on Web Elements using Locators in Selenium.

Note: The Selenium Actions class is useful for performing actions on any element on the screen by specifying x and y coordinates. It is possible to locate more than one web element using the Actions class.

Know more about Selenium concepts in this tutorial on Selenium WebDriver.

Using Action class in Selenium is of utmost importance in automated testing. This article simplifies the process so that testers know how to simulate common user actions on websites and applications. This lets them monitor software behavior in the real world so that they can verify and optimize user experience for its best possible state. 

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free