How to perform Advance activity in Selenium like- Mouse Hover, RightClick, DoubleClick, Keyboard Event

If you really want to automate critical applications, which include advance activity like Mouse Hover, Right click, Double click, Click and Hold, Keyboard activities and so on.

You cannot automate Ajax application, which contains advance activity so let us have a look.

you can also try Drag and Drop in Selenium using Action class

You do not have to worry about all this because all will come in single bundle i.e. you can perform all this using Actions class in Selenium.

Method name and Usage
moveToElement(WebElement)-- Mouse Hover
contextClick()-- Right click on page
contextClick(WebElement)-- Right click on specific Element
sendKeys(KEYS.ENTER)--For keyboard events
clickAndHold(WebElement)--Click on element and hold until next operation
release() Release the current control
We still have so many methods in Actions class so will recommend you have a look for other methods as well here for better clarifications.

Usage of Mouse Hover- Handle Autosuggestion in Selenium

Now a days its default feature of almost all the application take an example of Google itself when you type some words on search box, it gives some related suggestion.



To achieve this we will use first mouse hover on element then click.

Scenario for Naukri.com autosuggestion

First we will enter keywords using sendKeys() method then we have to wait for some time (2 or 3 seconds) to load suggestion and once it is loaded we will use mouse hover event using moveToElement() method of action class then we will click on that particular Item using click(Webelement) method of actions class.
Let’s implement the same

package com.qaearth;
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 MouseOver{
public static void main(String[] args) throws InterruptedException {
FirefoxDriver driver=new FirefoxDriver();
driver.get("http://www.naukri.com");
// Type something on Skill textbox
driver.findElement(By.id("qp")).sendKeys("test");
// Create object on Actions class
Actions builder=new Actions(driver);
// find the element which we want to Select from auto suggestion
WebElement ele=driver.findElement(By.xpath(".//*[@id='autosuggest']/ul/li[2]/a"));
// use Mouse hover action for that element
builder.moveToElement(ele).build().perform();
// Give wait for 1 seconds
Thread.sleep(1000);
// finally click on that element
builder.click(ele).build().perform();
}

}
Right Click in Selenium Webdriver

As we discussed earlier for right-click on a particular link or any web-element Selenium Webdriver has contextClick() methods available in Actions class.
There are two flavors of this
1-contextClick()- which will right Click on a page
2-contextClick(WebElement) – which will right click on a particular web-element.

Below is the program to right click on a link in Google Home page
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class TestRightClick {
@Test
public void TestClick() throws Exception{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com";);
driver.manage().window().maximize();
Actions act=new Actions(driver);
act.contextClick(driver.findElement(By.linkText(“à°¤ెà°²ుà°—ు”))).perform();
}
}
Keyboard events using Actions class.

For this we will use previous example after right click we will select second option from list for this we will use ARROW_DOWN key two times then we will hit ENTER Key.
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 GoogleTC {
public static void main(String[] args) {

WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
Actions act=new Actions(driver);
act.contextClick(driver.findElement(By.linkText("à°¤ెà°²ుà°—ు"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
}