Keyboard and Mouse event

Keyboard events

Selenium has an Actions class which provides different methods for Keyboard interactions. To perform action events, you need to use org.openqa.selenium.interactions Actions class. There are various ways to handle keyboard events:

  1. Handle Keyboard event using Action class
  2. Handle Keyboard event using sendKeys() command
  3. Handle Keyboard event using Robot class

Out of which first two are the mostly used which we will be covering further

1. Handle Keyboard event using Action class

The following are the most used keyboard events:

  1. keyDown(CharSequence key): Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) to Handle keyDown operation.
  2. keyDown(WebElement target, CharSequence key): Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) after focusing on an element.
  3. keyUp(CharSequence key): Performs a modifier key release (SHIFT,Keys.ALT or Keys.CONTROL) to Handle keyUp operation.
  4. keyUp(WebElement target, CharSequence key): Performs a modifier key release (SHIFT,Keys.ALT or Keys.CONTROL) after focusing on an element.
  5. sendKeys(CharSequence keys): Sends keys to the active element.
  6. sendKeys(WebElement target, .CharSequence keys): Sends keys to the given element using sendKeys.

These methods can be used through following:

Step1: Import the Actions classes.

import org.openqa.selenium.interactions.Actions;

Step2: Instantiate an Actions class:

            Actions actions = new Actions(driver);

Step3: Build and Perform the action sequence

            actions.<keyboard event method>.buid().perform();

Example:  

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

// Sendkeys using Action class object

actions.sendKeys(Keys.ENTER).build().perform();

// Send multiple keys using action class
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform()

2. Handle Keyboard event using sendKeys() command

This can be achieved through a very simple way

Actions actions = new Actions(driver);
actions.sendKeys(userName, "Mr.Jones").perform();
 


Mouse Event

The following are the most used keyboard events:

  1. doubleClick (): Double clicks onElement
  2. clickAndHold(): Clicks at the present mouse location (without releasing)
  3. contextClick(): Performs a context-click (right click) on an element
  4. dragAndDrop(source, target): It Invokes click-and-hold at the source location and moves the object(document, files etc.) to the target location of the element before releasing the mouse.
  5. moveToElement(toElement): It shifts the mouse to the center of the element
  6. release(): It releases the depressed left button of the mouse at the existing mouse location


These methods can be used through following:

Step1: Import the Actions classes.

import org.openqa.selenium.interactions.Actions;

Step2: Instantiate an Actions class:

            Actions actions = new Actions(driver);

Step3: Build and Perform the action sequence

            actions.<keyboard event method>.buid().perform();

 Example: 

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

WebElement username = driver.findElement(By.id(“a”));

// Move to element and perform click
builder.moveToElement(userName).click().perform();
 
Mouse & Keyboard event can be used together as below:
Actions builder = new Actions(driver);

//Perform keyboard and mouse event
builder.moveToElement(userName).
click().                       //click on element
keyDown(userName, Keys.SHIFT). //This will type in uppercase
sendKeys("Hello").             //type hello
build().perform();