Advanced User Interactions in Selenium

It is common that we execute some basic Selenium operations in application like click an element, select a value from dropdown, enter a value, clear the value etc.
Introduction

It is common that we execute some basic Selenium operations in application like click an element, select a value from dropdown, enter a value, clear the value etc.

We can also execute some advanced actions like double click on element, drag and drop the element, mouse hover on an element, clicking multiple elements while holding down the Control key, etc.

The Action class methods implement these advanced user actions.

To access these methods, create an object for the Action class and pass the driver object as an argument to the object in which you are going to execute the advanced user actions.

Example:        Actions a = new Actions (driver);

Actions class methods for Mouse interactions

1. Click ()

Used to Click an element. Equivalent to WebElement.click().

2. ContextClick()

Clicking the mouse button that brings up the contextual menu (Right click).

3. DoubleClick()

double-clicking an element.

4. ClickAndHold()

Holding down the left mouse button

5. MoveToElement()

Moving the mouse from its current location to another element

6. Release()

Releasing a held mouse button.

7. MoveByOffset()

Moving the mouse to an offset from an element (The offset could be negative and the element could be the same element that the mouse has just moved to).

8. DragAndDrop()

To drag an element and drop it on another element.

9. DragAndDropBy()

Moving the element to an offset from an element

Keyboard interactions

1. SendKeys()

Equivalent to WebElement.sendKey(“input value”)

2. KeyDown()

Holding down a modifier key. Sends a key press only, without releasing it. Should only be implemented for modifier keys (Control, Alt and Shift).

3. KeyUp()

Releasing a modifier key.

Sample steps

Step 1:

Create the object for driver

WebDriver driver = new FirefoxDriver();

Step 2:

Create an object for Actions class to perform the advanced user interactions.

Actions a = new Actions(driver);

Step 3:

Access the click method and give the WebElement where you want to click; otherwise the tool will click on the page where the cursor is available.

a.click(d. findElement(By.id(“Email”))).perform();

Example program for Actions class
import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Actions;

public class AdvancedUserActions {

public static void main(String[] args) throws InterruptedException {

WebDriver d = new FirefoxDriver();

d.manage().timeouts().implicitlyWait(20000, TimeUnit.MICROSECONDS);

d.get(“https://stage.mstsolutions.com/”);

Actions a = new Actions(d);

// Normal click

a.click(d.findElement(By.linkText(“LET’S GET STARTED”))).perform();

// Right click on username field

a.contextClick(d.findElement(By.id(“first_name”))).perform();

Thread.sleep(2000);

// Press Escape key

a.sendKeys(Keys.ESCAPE).perform();

// Enter username in upper case

a.keyDown(Keys.SHIFT).sendKeys(“user”).build().perform();

a.keyUp(Keys.SHIFT).perform();

}

}

Program for Mouse Hover operation
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Actions;

public class AdvancedUserActions {

public static void main(String[] args) throws InterruptedException {

WebDriver d = new FirefoxDriver();

d.manage().timeouts().implicitlyWait(20000, TimeUnit.MICROSECONDS);

d.get(“https://stage.mstsolutions.com/”);

Actions a = new Actions(d);

// Mouse hover on “About us”

a.moveToElement(d.findElement(By.id(“menu-item-5”))).perform();

}

}

Generating Action chains
  • In order to generate a sequence of actions, use the Actions generator to build it.

Example: I want to drag an element and drop it on another element.

  • To perform this operation, we need to use three methods.
  1. clickAndHold()
  2. moveToElement()
  3. release()
  • To generate the sequence of actions, finally use the build method.

Actions a = new Actions(driver); 

a.clickAndHold(someElement).moveToElement(otherElement).release(otherElement).build().perform();

How to Drag and Drop an Element in Selenium WebDriver
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;

public class AdvancedUserActions {

public static void main(String[] args) throws InterruptedException {

WebDriver d = new FirefoxDriver();

d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Actions a = new Actions(d);

// Login into Sales force

d.get(“https://login.salesforce.com/”);

d.findElement(By.cssSelector(“input[class=’input r4 wide mb16 mt8 username’]”)).sendKeys(“salesforce@mail.com”);                d.findElement(By.cssSelector(“.input.r4.wide.mb16.mt8.password”)).sendKeys(“password1”);

d.findElement(By.cssSelector(“#Login”)).click();

// Click on Reports object

d.findElement(By.linkText(“Reports”)).click();

// Click on New Dashboard

d.findElement(By.cssSelector(“input[value=’New Dashboard…’]”)).click();

// Drag a donut chart and drop into dashboard

// Method 1

WebElement element1 = d.findElement(By.id(“analytics:chart-donut”));

WebElement element2 = d.findElement(By.id(“ext-gen146”));

a.dragAndDrop(element1, element2).perform();

// Method 2

WebElement element11 = d.findElement(By.id(“analytics:chart-pie”));

WebElement element22 = d.findElement(By.id(“ext-gen143”));

a.clickAndHold(element11).moveToElement(element22).click().release().build().perform();

}

}

Select jQuery Selectable Items Using Actions Class of Selenium WebDriver
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;

public class AdvancedUserActions {

public static void main(String[] args) throws InterruptedException {

System.setProperty(“webdriver.chrome.driver”,

“C:\\Users\\MSTSYS0059\\Desktop\\Selenium training\\chromedriver.exe”);

WebDriver d = new ChromeDriver();

d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Actions a = new Actions(d);

// Login into Sales force

d.get(“https://login.salesforce.com/”);

d.findElement(By.cssSelector(“input[class=’input r4 wide mb16 mt8 username’]”))

.sendKeys(“salesforce@mail.com”);

d.findElement(By.cssSelector(“.input.r4.wide.mb16.mt8.password”)).sendKeys(“password1”);

d.findElement(By.cssSelector(“#Login”)).click();

// Click on Reports object

d.findElement(By.id(“setupSearch”)).sendKeys(“Apps”);

d.findElement(By.linkText(“Apps”)).click();

d.findElement(By.xpath(“.//*[@id=’bodyCell’]/div[4]/div[1]/div/div[2]/table/tbody/tr[8]/td[1]/a[1]”)).click();

// Select four options from the first list box

WebElement element1 = d.findElement(By.xpath(“.//*[@id=’duel_select_0′]/option[9]”));

WebElement element2 = d.findElement(By.xpath(“.//*[@id=’duel_select_0′]/option[10]”));

WebElement element3 = d.findElement(By.xpath(“.//*[@id=’duel_select_0′]/option[11]”));

WebElement element4 = d.findElement(By.xpath(“.//*[@id=’duel_select_0′]/option[12]”));

a.keyDown(Keys.CONTROL).click(element1).click(element2).click(element3).click(element4).keyUp(Keys.CONTROL).build().perform();

Thread.sleep(2000);

// Add the selected list box elements to the list box 2 side

d.findElement(By.xpath(“//img[@title=’Add’]”)).click();

}

}

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.