Selenium

Selenium Keyboard Events Example

Photo of Sarad DhungelSarad DhungelJanuary 25th, 2017Last Updated: April 8th, 2019
1 198 2 minutes read

1. Introduction

In this tutorial we will be discussing about the Advance User Interactions API. Using these APIs, we can perform actions on a web page similar to a user would interact on the user using keyboard and mouse.
 
 
 
 
 
 
 

 
The Actions interface and Action classes are two modules of the API that needs to be implemented to make use of keyboard and mouse events like Drag and Drop or selecting multiple elements with Control Key.

2. Action interface and Actions Class

Actions class implement the Action interface. Action interface only one method: perform(). The action method gets the arguments as constructor and then implementing class decides what interaction should be done on the webpage. For example finding an Element, passing keys using sendkeys and highlighting it.

We can implement action actions methods by importing org.openqa.selenium.interactions.Actions and org.openqa.selenium.interactions.Action

Then configure it by creating an object of Actions class like below:
Actions build = new Actions(driver);
Build.moveToELement(toElement).click().perform();

3. Available actions for Keyboard and Mouse Events

  • ButtonReleaseAction – Releases pressed mouse button.
  • ClickAction – clicks on an element similar to WebElement.click().
  • ClickAndHoldAction – holds the left mouse button.
  • ContextClickAction – similar to clicking on right button contextual menu
  • DoubleClickAction– similar to double clicking.
  • KeyDownAction – similar to holding modifier keys like tab, shift and control
  • KeyUpAction – releases modifier keys.
  • MoveMouseAction – similar to moving the mouse from current location to another.
  • MoveToOffsetAction – moves the mouse from one location to another using x and y offset.
  • SendKeysAction – similar to WebElement.sendKey() to send keys in form of sequence of characters.

In order to achieve different operations that are performed by actions classes, we can implement the methods available for keyboard and mouse interfaces.

3.1 Methods of Keyboard Interface:

  1. sendKeys(onElement, charSequence) – send keys to the browser similarly a user would do using keyboard. Keys are send in forms of sequence of characters.
  2. pressKeys() – used to send special keys of the keyboard like “f1”, “shift”, “tab” etc.
  3. releaseKey()– releases key on the keyboard and presskeys.

KeyboardsEvents.java

package com.javacodegeeks.SeleniumKeyboardEvents;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.Action;import org.openqa.selenium.interactions.Actions;public class KeyboardEvents {public static void main(String[] args) {      String exePath = "/Users/saraddhungel/Downloads/chromedriver";     System.setProperty("webdriver.chrome.driver", exePath);         WebDriver driver = new ChromeDriver();     driver.get("http://www.google.com/"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement text = driver.findElement(By.name("q")); Actions make  = new Actions(driver); Action kbEvents = make.keyDown(text, Keys.SHIFT).sendKeys("Java Code Geeks")    .keyUp(text, Keys.SHIFT).doubleClick().contextClick().build();kbEvents.perform();}}

The “Java Code Geeks” keyword is sent to the search box of the google site. Thus, it is changed to UPPERCASE withKeyDown() method anddoubleClick() method double clicks on it to highlight the text andcontextClick() does event of double click on the text and thus contextual menu is displayed.

Want to master Selenium?
Subscribe to our newsletter and download theSeleniumProgramming Cookbookright now!
In order to get you prepared for your Selenium development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

4. Conclusion

This example set was a demonstration of the Advance User Interactions API available in Selenium WebDriver and how can we use them to make use of Keyboard and Mouse events on the web similar to user’s interaction to web.

5. Download the Eclipse Project

This was an example of Selenium Keyboard Event

Download
You can download the source code of this example here:Selenium Keyboard Event
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Sarad DhungelSarad DhungelJanuary 25th, 2017Last Updated: April 8th, 2019
1 198 2 minutes read
Photo of Sarad Dhungel

Sarad Dhungel

I am a graduate in Computer Engineering from Howard University. Third place award winner in Intel-Cornell Cup. Passionate about new emerging software and technology. During free time, I enjoy reading about politics, business, spirituality, technology and traveling.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.