Selenium WebDriver - Actions Class Keyboard Events

This tutorial is part of the Selenium Webdriver Commands series. In this tutorial we will cover Actions class and its commands.

Actions Class
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 actions such as mouse hover, double click, right click, drag & drop and clicking multiple elements.

Create Object of Actions class:
// Create object of Actions class
Actions actions = new Actions(driver);

It's commands can be broadly classified in two groups:
  1. Keyboard Events
  2. Mouse Events
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 Example 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.

Code Example:
/* 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();


You will learn about Mouse events in next tutorials.

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();
  
 }
}



Click here to find complete Selenium Webdriver Tutorial Series



Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Comments

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Top 25 Must to know Selenium Webdriver Commands List

Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

Selenium WebDriver - Browser and Navigation Commands

Selenium Essentials - How to become expert in Automation Testing?