Keyboard Events Operations in Selenium WebDriver

This is another tutorial on advanced Selenium learning. We will again take help of Actions class while handling various Keyboard events in Selenium WebDriver. Till now we have discussed, drag and drop operations, right-click operations, double-click operations and mouse hover operations. These are the prominent Selenium operations which we have to perform our Test automation journey.

Today we will discuss the scenario where we have to press keys on the keyboard through Selenium WebDriver. Let me explain this scenario with an example.

We are running a test case in which we have to press enter or we have to perform shift press operation, so in such case, we will call the methods which are responsible to press and release keys on the keyboard. Sometimes this is also useful when we have a scenario to upload files on the web applications.

Suggestions:

Meanwhile, you can check our exclusive post on keyboard operations through Robot Class

Technical Specifications of Keyboard events

  • We have to call interactions package of the Selenium WebDriver
  • Package: org.openqa.selenium.interactions
  • Further, we can proceed with creating the instance of the Actions class

Keyboard Events

What are the responsible methods to perform Keyboard Events operations in Selenium?

Following methods are responsible to perform Keyboard events operations in Selenium:

keyDown(Keys Data)

This method allows you to choose keys of the keyboard and then press the key. You can give the command to press keys like SHIFT, ALT, ENTER, BACKSPACE, CONTROL, etc. Its technical declaration will be- keyDown(java.lang.CharSequence Key)

Here is the syntax for this method:

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations

act.keyDown(Keys.SHIFT) .build().perform();

This method will perform key press operation without focussing on any specific web elements.

keyDown(WebElement, Keys Data)

This method works similar to above, but it allows us to key press by specifying a web element. Its technical declaration will be- keyDown(WebElement target, java.lang.CharSequence Key)

Here is the syntax for this method:

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations on specific WebElement

WebElement ele = driver.findElement(By.xpath(xpath_expression));

act.keyDown(ele, Keys.SHIFT) .build().perform();

keyUp(Keys Data)

This method performs the opposite operation which was performed by keyDown(). It is also an obvious fact that we need to release the key which we have pressed so once we use keyDown() then we have to release it by calling keyUp() method. Hence, both these methods for key press & key release will be used one by one. Its technical declaration will be- keyUp(java.lang.CharSequence Key)

Always release the same key which you have pressed, like if you have pressed shift then you have to release shift only.

Here is the syntax for keyUp():

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations

act.keyUp(Keys.SHIFT) .build().perform();

keyUp(WebElement, Keys Data)

Its operation will be opposite of keyDown() method with WebElement as an argument. It will release the key on specific web element. Its technical declaration will be- keyUp(WebElement target, java.lang.CharSequence Key)

Here is the syntax for this method:

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations on specific WebElement

WebElement ele = driver.findElement(By.xpath(xpath_expression));

act.keyUp(ele, Keys.SHIFT) .build().perform();

sendKeys(Keys Data)

This method processes strings and keys as well. Here we don’t need to release the keys. Its technical declaration will be- sendKeys(java.lang.CharSequence… Key)

Here is the syntax for this method:

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations

act.sendKeys("hello", Keys.ARROW_DOWN) .build().perform();

or, act.sendKeys("hello") .build().perform();

sendKeys(WebElement, Keys Data)

This method acts like clicking on an element first then sending the string to the application. This method also focuses on the specific web element to send the stream of the character sequence. Its technical declaration will be- sendKeys(WebElement target, java.lang.CharSequence… Key)

Click here to know more if you have any confusions with the sendKeys() which you were using previously to send simple strings.

//Instance of Actions class

Actions act = new Actions(driver);

//Keyboard events operations on specific WebElement

WebElement ele = driver.findElement(By.xpath(xpath_expression));

act.sendKeys(ele, Keys.SHIFT, “hello”) .build().perform();

Or

act.sendKeys(ele, “hello”) .build().perform();

Test Steps

Step# 1: Open Browser

Step# 2:  Type in search box using sendKeys()

Step# 3: Validate output

Sample Program

Following sample program first convert text in BLOCK LETTER then sends to the search box and it displays the result.

Keyboard Events

package SeleniumTester;

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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class KeyboardEventsOperations {
  @Test
  public void keyboardEventsOperations() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("https://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  
	  WebElement dbl = driver.findElement(By.xpath("//*[@id='s']"));
	  Thread.sleep(3000);
	  
	  //Instance of Actions class
	  Actions act = new Actions(driver);
	  
	  //Keyboard events operations
	  act.sendKeys(dbl, Keys.SHIFT, "selenium").build().perform();
	  
	  Thread.sleep(3000);
	  
	  driver.close();
	  
	  driver.quit();
  }
}

 

Leave a Reply