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