Action Class in Selenium Webdriver to perform Keyboard and Mouse Events

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:
  1. Keyboard Events
  2. 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:

i. keyDown(KeyCode)  - Performs key press without releasing it.
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();



Complete 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 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:
  1. Creating object of Actions class as actions. And passing driver object to constructor of Actions class.
  2. In second line, creating an object of WebElement and passing Xpath locator to it.
  3. moveToElement() is the command to perform mouse hover action.
  4. 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();

3. Context Click (Right Click)
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, 

Happy Learning!

Selenium Webdriver Commands  << Previous   ||   Next >>   Selenium Wait Commands

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

How to Handle Dynamic Web Table in Selenium WebDriver

Top 50 Selenium Webdriver Interview Questions

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation