Actions class in Selenium Web Driver || Mouseover Event, Mouse RightClick, Drag and Drop

Actions class in Selenium Web Driver.

Sometimes we need to perform automation operation with mouse and keyboard like mouseover, right click from the mouse. To perform this kind of keyboard and mouse event we need to use Actions class in selenium web driver.

In order to use Actions class in selenium web driver, we need to use org.openqa.selenium.interactions

Library. It contains the Actions and the Action classes that are needed when executing these events. We can perform following operation in an easy way using Actions class. In the below examples we will discuss all the event one by one in detail.

Mouseover Event:

When mouse pointer moves on to the element or child element then mouseover event occurs. We just need to move the mouse point no need to click on the element and the action perform without click just needs to over the mouse on the particular elements.

How to Automate Mouseover event in selenium web driver with Java.

To perform a Mouseover event with selenium web driver We will use Actions class from org.openqa.selenium.interactions library. In the below script a simple example to perform a mouseover event with selenium web driver.

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;



public class ActionClassExamples {

public static WebDriver driver;



/*

* ................Task description...............

* URL= http://codebun.com

* Mouseover at Selenium in the menu.

* Click on katalon Studio.

* Selenium=>Katalon studio.

*/



public static void main(String[] args) {



//Set the system property.

System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");

//Create an instance of Chrome driver.

driver = new ChromeDriver();

//Load URL.

driver.get("http://codebun.com");

WebDriverWait wait = new WebDriverWait(driver, 60);

//Wait until driver is not locate the path of "Selenium".

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='menu-item-1368']/a")));

//Get the elements is the variable to make the task easy.

WebElement seleniumTab = driver.findElement(By.xpath("//*[@id='menu-item-1368']/a"));

WebElement KatalonStudioTab = driver.findElement(By.xpath("//*[@id='menu-item-1196']/a"));

//Create an object of Actions class and pass the driver as parameter.

Actions act = new Actions(driver);

//mouse over to the seleniumTab.

act.moveToElement(seleniumTab).build().perform();

//Wait to load Katalon studio in sub menu.

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='menu-item-1196']/a")));

//Click to the Katalon studio.

act.moveToElement(KatalonStudioTab).click().build().perform();

//KatalonStudioTab.click();

driver.close();



}



}

Mouse RightClick

Sometimes we need to perform right click operation in selenium. To perform action form mouse and keyword we will use Actions class in selenium. Below is the simple example to perform right click operation is selenium web driver with java.

How to perform right click operation Selenium Web Driver

Scenario to be automated:

Open Browser

Launching the website URL to automate.

Find the element(Site Name Heading) by id or XPath it’s up to you.

Right click on the element.

Close browser.

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;



public class RightClick {



public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");



// Open Browser



WebDriver driver = new ChromeDriver();



// Launching web site URL to automate.

driver.get("http://www.seleniumeasy.com/test/");



// Pass the driver object to the Action class.

Actions action = new Actions(driver);



// Find the element by id or xPath its up to you.

WebElement siteLogo = driver.findElement(By.id("site-name"));



// Pass the element to contextClick method and perform operation using object of

// action class.

action.contextClick(siteLogo).perform();



// Close browser.

// driver.close();

}

}

Drag and Drop

 In drag and drop, we need to interact with two elements. The first element will be the source(Which we need to drag) and the other will be the destination(Where we need to drop the element). In the below script simple example to perform drag and drop operation with selenium web driver with java.

How to perform Drag and Drop operation Selenium Web Driver

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;



public class DragAndDropExample {

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



System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");



WebDriver driver = new ChromeDriver();



// Open webpage

driver.get("http://jqueryui.com/resources/demos/droppable/default.html");



// Create an object of actions class

Actions act = new Actions(driver);



// find element which we need to drag(Source element)

WebElement dragElement = driver.findElement(By.xpath(".//*[@id='draggable']"));



// find element which we need to drop(destination element)

WebElement dropElement = driver.findElement(By.xpath(".//*[@id='droppable']"));



// Wait for 3 sec to check properly

Thread.sleep(3000);



// Perfrom drag and drop.

act.dragAndDrop(dragElement, dropElement).build().perform();

}



}

Resizable Elements

.

Add a Comment

Your email address will not be published. Required fields are marked *