PAGE 12: HOW TO HANDLE KEYBOARD AND MOUSE EVENTS USING SELENIUM WEBDRIVER

In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.
In order to perform action events, we need to use org.openqa.selenium.interactions.Actions class.


The following are the keyboard  and  mouse events :

Method :clickAndHold()
Purpose: Clicks without releasing the current mouse location

Method : contentClick()
Purpose: Performs a context-click at the current mouse location.
How to work with context menu by taking a simple example

Method: doubleClick()
Purpose: Performs a double click at the current mouse location

Method: dragAndDrop(source,target)
Parameters: Source and Target
Purpose: Performs click and hold at the location of the source element and moves to the location of the target element then releases the mouse.

Method : dragAndDropBy(source,x-offset,y-offset)
Parameters: Source, xOffset - horizontal move, y-Offset - vertical move Offset
Purpose: Performs click and hold at the location of the source element moves by a given off set, then releases the mouse.

Method: keyDown(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a modifier key press, doesn't release the modifier key. Subsequent interactions may assume it's kept pressed

Method: keyUp(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a key release.

Method: moveByOffset(x-offset, y-offset)
Parameters: X-Offset , Horizontal offset, a negative value means moving the mouse to left side.
Y-Offset, vertical offset, a negative value means moving the mouse to up.
Purpose: Moves the mouse position from its current position by the given offset.

Method: moveToElement(toElement)
Parameters: toElement - Element to Move to
Purpose: It moves the Mouse to the middle of the element.

Method: release()
Purpose: It releases the left mouse button at the current mouse location.

Method: sendKeys(onElement, charSequence)
Parameters: onElement, which will receive the keyStrokes, usually text field.
charsequence- any string value representing the sequence of keyStrokes to be sent.
Purpose: It sends a series of keyStrokes onto the element


EXAMPLE:

Below are the Steps we have followed in the example:

1. Identify the element
2. Wait for the presence of Element
3. Now perform Context click

package com.keyboard;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class KeyboardFunction {

WebDriver driver;
String URL = "http://medialize.github.io/jQuery-contextMenu/demo.html";

@Test
public void rightClickTest() {
driver = new FirefoxDriver();
driver.navigate().to(URL);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement locator = driver.findElement(By.xpath("//span[@class='context-menu-one btn btn-neutral']"));
rightClick(locator);

}

public void rightClick(WebElement element) {
try {
Actions action = new Actions(driver).contextClick(element);
action.build().perform();
System.out.println("Successfully Right clicked on the element");

} catch (Exception e) {
e.printStackTrace();
System.out.println("Unable to right click on to the element");
}

}
}


Executing test via TestNG.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Amrit Script">
<test name="Keyboard actions">
<classes>
<class name="com.keyboard.KeyboardFunction" />
</classes>
</test>
</suite>


OUTPUT:

Successfully Right clicked on the element

===============================================

EXAMPLE -2

package com.keysCommands;

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.testng.annotations.Test;

public class KeysCommands {

WebDriver driver;

@Test
public void keyCommandExample1() throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
WebElement FirstName = driver.findElement(By.id("u_0_1"));
FirstName.sendKeys(Keys.TAB);
WebElement LastName = driver.findElement(By.id("u_0_3"));
LastName.sendKeys(Keys.F5);

}

}


Executing test via TestNG.xml:

package com.keysCommands;

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.testng.annotations.Test;

public class KeysCommands {

WebDriver driver;

@Test
public void keyCommandExample1() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
WebElement FirstName = driver.findElement(By.id("u_0_1"));
FirstName.sendKeys(Keys.TAB);
WebElement LastName = driver.findElement(By.id("u_0_3"));
LastName.sendKeys(Keys.F5);

}

}

OUTPUT:


===============================================
Keys Commands in Selenium WebDriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================


MOUSE COMMAND EXAMPLE:

package com.mediaocean.mouse;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class MouseCommand {

WebDriver driver;

@Test
public void mouseCommandsExample() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/p/mouse-hover.html");

Actions actions = new Actions(driver);
WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div"));
actions.moveToElement(moveonmenu).moveToElement(driver.findElement(By.xpath("//div[@id='menu1choices']/a")))
.click().perform();
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("Google"));

}

}


Executing test via TestNG.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Mouse Hover Commands in Selenium WebDriver">
<test name="Mouse Hover Commands Test">
<classes>
<class name="com.mediaocean.mouse.MouseCommand" />
</classes>
</test>

</suite>


OUTPUT:
===============================================
Mouse Hover Commands in Selenium WebDriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================


1 comment:

Unknown said...

Nice blog..Thanks for sharing this valuable information. i suggest this blog to my friends.Keep update Top selenium training institutes in chennai

Selenium Testing Course
software testing course in velachery chennai

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com