Keyboard Events Operations in Selenium WebDriver

Category: Selenium WebDriver 55 0

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:

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:

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():

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:

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:

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.

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

 

Related Articles

Add Comment