What is Actions Class in Selenium WebDriver?
In this tutorial we will cover Action class and its commands. It is considered as the Advanced Selenium Commands.
What is Action Class in Selenium WebDriver?
Actions class is used to handle Keyboard and Mouse Events. You need to import org.openqa.selenium.interactions.Actions in order to use Actions class. This class includes keyboard and mouse actions such as double click, right click, drag & drop, mouse hover and clicking multiple elements.
It's commands can be broadly classified in two groups:
- Keyboard Events
- Mouse Events
Create object of Actions class:
// Create object of Actions class Actions actions = new Actions(driver);
1. Keyboard Events
Listing down some of the methods to perform keyboard events:
Parameters - Key_Code For e.g., Keys.ALT, Keys.SHIFT or Keys.CONTROL
ii. keyUp(KeyCode) - Performs a key release. It has to be used after keyDown to release the key.
Parameters - Key_Code For e.g., Keys.ALT, Keys.SHIFT or Keys.CONTROL
Code for keyUP and keyDown:
/* KeyUP and KeyDown */ // Create object of Actions class Actions actions = new Actions(driver); // Locate Web Element WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath")); // This will type text in Uppercase as we are typing using Shift key pressed actions.keyDown(Keys.SHIFT) .sendKeys(PRODUCT_CATEGORY, "casual wear") .keyUp(Keys.SHIFT) .build() .perform();
iii. sendKeys(keysToSend) - Sends series of keystrokes to the active web element which is generally a text box. In simple language it is used to enter text in a text box.
Parameters - Text to be entered.
/* Send Keys - Enter Text */ // Create object of Actions class Actions actions = new Actions(driver); // Locate Web Element WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath")); actions.sendKeys(PRODUCT_CATEGORY, "Casual Wear").perform();
Program for Actions Class Keyboard Events:
package com.techlistic.selenium; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class ActionsKeyboard { public static void main(String[] args) { // Set Path of Gecko driver System.setProperty("webdriver.gecko.driver", "./src/com/techlistic/utils/geckodriver.exe"); // Launch Forefox WebDriver driver = new FirefoxDriver(); // Open Techlcistic.com's practice form driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); // Set Implicit Wait driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // Maximize Window driver.manage().window().maximize(); // Store Location of Firstname in Webelement WebElement FIRSTNAME = driver.findElement(By.name("firstname")); /* KeyUP and KeyDown */ // Create object of Actions class Actions actions = new Actions(driver); // This will type Username in Uppercase as we are typing using Shift key pressed actions.keyDown(Keys.SHIFT) .sendKeys(FIRSTNAME, "john snow") .keyUp(Keys.SHIFT) .build() .perform(); } }
2. Mouse Events
In this section of the tutorial we will learn Mouse event commands of Actions class in Selenium WebDriver. Some of the mouse events that can be performed using Actions class are:
- click()
- doubleClick()
- contextClick()
- clickAndHold()
- dragAndDrop()
- moveToElement()
- moveByOffset(x, y)
- release()
1. Mouse Hover & Click
We usually come across some scenarios in automation of mouse hover like clicking on a sub-menu link which appears only after moving your cursor to menu link. It's quite a tricky thing to perform in automation. Thanks to the Actions Class in Selenium WebDriver. We already have a Selenium command for it.
Code Example for Mouse Hover:
/* Mouse Hover */ // Xpath for Menu WebElement Menu_Link = driver.findElement(By.xpath("/html/some/xpath")); // Xpath for Sub Menu WebElement SubMenu_Link = driver.findElement(By.xpath("/html/some/xpath")); // Create object of Actions class Actions actions = new Actions(driver); // Move cursor to Menu link (Mouse hover on menu link so that sub menu is displayed) actions.moveToElement(Menu_Link); // Click on Submenu link (whcih is displayed after mouse hovering curson on menu link) actions.click(TSHIRTS_Submenu_Link).build().perform();
Explanation of above code:
- Creating object of Actions class as actions. And passing driver object to constructor of Actions class.
- In second line, creating an object of WebElement and passing Xpath locator to it.
- moveToElement() is the command to perform mouse hover action.
- actions.build().perform() - This code is mandatory to finish any actions class command. build() method combines multiple actions into one single action and perform() executes those actions.
2. Double Click
This command is used to double click on an element.
Code for Double Click:
/* Double Click */ Actions actions = new Actions(driver); WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath")); actions.doubleClick(PRODUCT_CATEGORY); actions.build().perform();
This command is used to right click on an element.
Code for Context click (right click):
/* Right Click */ // Locate web element WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath")); // Create object of actions class Actions actions = new Actions(driver); // Right click on PRODUCT_CATEGORY element actions.contextClick(PRODUCT_CATEGORY); actions.build().perform();
4. Drag and Drop
This command is for drag and drop a web element and is rarely used.
Code for Drag and Drop:
/* Drag And Drop */ // Weblements for source and target WebElement source = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); // Create object of actions class Actions action = new Actions(driver); action.dragAndDrop(source, target); action.build().perform();
*Assignment Time*
Now it's time to do a coding assignment,
Selenium WebDriver Commands Tutorial << Previous || Next >> Selenium Wait Commands
Follow Techlistic
Feel free to ask queries or share your thoughts in comments or email us.
Comments
Post a Comment