Features

How to handle Action class in Selenium

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

Selenium WebDriver comes with an Action Class, which allows you to simulate user input events, such as mouse and keyboard actions.

This article will give you an overview of what Selenium Actions can do, and how to use them in your tests.

What are Selenium Actions?

Selenium Actions allow you to perform UI actions in your test.
Clicking, double-clicking, hovering or other complex mouse actions can be scripted with an action.

Advanced user interactions such as holding a key while clicking something, or dragging and dropping an item are supported with Selenium Actions.

These actions are performed by the Advanced User Interactions API, which consists of the Selenium Action class to perform these interactions.

How does the Selenium Action Class work?

The Selenium Action class is able to perform 2 types of user input: mouse actions and keyboard actions.

Mouse Actions

The mouse actions will mimic various actions that a user can do on your website:

  • click(): clicks at the current mouse location
  • doubleClick(): double click at the current mouse location
  • contextClick(): right click at the current mouse location
  • dragAndDrop(WebElement source, WebElement target): drags an element from one location to the other
  • moveToElement(WebElement target): moves an element

Keyboard Actions

The keyboard actions will mimic various keyboard actions that a user can do on your website:

  • keyUp(WebElement target, java.lang.CharSequence key): does a key release after focusing on the target element
  • keyDown(WebElement target, java.lang.CharSequence key): does a key press after focusing on the target element
  • sendKeys(WebElement target, java.lang.CharSequence... keys): types a sequence of keys.

There are more methods available in the Action class, please see this overview of Actions class documentation page.

To use the Selenium Actions class in your Java test project, please import the necessary class:

import org.openqa.selenium.interactions.Actions

How to perform mouse actions with Selenium WebDriver?

You can perform several mouse interactions with the Selenium Action class, including: hovering, clicking and double-clicking.

Please see the example below where we'll perform several mouse actions in our Java test case.
We'll find an element through Xpath, hover over the element and finally double click it.

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:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();

       	driver.get("https://testingbot.com/");

        Actions ac = new Actions(driver);

        // Find the element we want to interact with

		WebElement freeTrialAction = driver.findElement(By.xpath("/html/body/div[2]/nav/div/div[3]/a[1]"));

        // Hover over the element

        ac.moveToElement(element).build().perform();
        // Double click on element

		a.doubleClick(freeTrialAction).perform();

		driver.quit();  
   }
}

How to perform keyboard actions with Selenium?

Selenium Actions allow you to perform keyboard operations such as scrolling up and down the page, copy and pasting (via keyboard)

In the example below we'll show how to do various actions with the keyboard during your test.

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:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();

       	driver.get("https://testingbot.com/");

        Actions ac = new Actions(driver);

        // Find the element we want to interact with

		WebElement freeTrialAction = driver.findElement(By.xpath("/input"));

        // Type something with SHIFT pressed down (uppercase)

        ac.keyDown(element, Keys.SHIFT).sendKeys("testingbot").build().perform();
        // Scroll Down using the CTRL+END keys

		ac.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
		// Paste the clipboard contents - depending on the OS under test, this may use different keys

		ac.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).build().perform();

		driver.quit();  
   }
}

How to combine multiple actions with the Selenium Actions class?

Because Selenium Actions uses the builder pattern, it's possible to chain multiple actions together.
This means you can create complexer user input actions, a series of actions.

Please see the example below where the test will press the SHIFT key, type something, release the shift key, double-click an element and right-click - all in one action.

Actions builder = new Actions(driver);
Action seriesOfActions = builder
	.moveToElement(textField)
	.click()
	.keyDown(txtUsername, Keys.SHIFT)
	.sendKeys(txtUsername, "testingbot")
	.keyUp(txtUsername, Keys.SHIFT)
	.doubleClick(txtUsername)
	.contextClick()
	.build();
	
seriesOfActions.perform();
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Handling Captcha during Automated Testing

We will discuss possible solutions to run automated tests with Captcha enabled websites.

Read more
Cypress and Cucumber Browser Testing

Cypress and Cucumber is a great combination to write clean and precise end-to-end browser tests.

Read more
TestNG automation with Selenium

TestNG is a popular Java Test Framework which allows you to write clean Selenium tests with built-in parallelisation.

Read more

Why would you choose TestingBot instead of an other company like SauceLabs or BrowserStack?

Read more

With a large number of different browsers platforms and mobile devices it is very important to make sure your website looks and behaves the way you want it to across all these different browsers.

Read more

A single piece of botched code in the wrong place can cost your company millions of dollars, and if it's not reported in good time, it can potentially break the back of your business.

Read more