Right Click operations in Selenium (Example, SAP Hybris)

When I was writing a Selenium Automation code then I found that the application had some functionality to perform on right click. Let me tell you about the application. It was SAP Hybris application and it had the functionality to create a new file on right click. So first I had to perform right click operations then I captured the web element of the option which was allowing creating a new file. In such case, right click command of Actions class helped a lot.

Here is the test case for that SAP Hybris application.

Step#1: Login to Hybris Management Console (HMC)

Step#2: Validate dashboard

Step#3: Click on the options available on the dashboard

Step#4: Each option has choices to create a new file; therefore, if you wish to create a new file then perform right click operations and then click on create a new file

Step#5: Validate the new file successfully created

I gave you real time example from one of my projects; It will help you, whenever, you get any of the related projects in future.

Technical Specifications of Right click operations in Selenium

Let me tell you some technical specifications of the right click operations in Selenium WebDriver.

  • Technically, this is called as Context menu
  • We need to create an instance of the Actions class again to use context menu command
  • Import Selenium’s interactions package
  • Package: org.openqa.selenium.interactions.Actions

Syntax of Right click operations

//Instance of Actions class

Actions act = new Actions(driver);

//Right click command

act.contextClick(ele).build().perform();

Here, we first create the instance of Actions class, and then we call the contextClick() method to perform the right click operations on any web elements. One thing you should not miss to pass the WebElement as an argument within the contextClick() method.

If we specify any web element as an argument in the contextClick() method then driver points at the middle of the element by default and then it performs right click at that middle location there.

right click operations

Suppose if there is a scenario where we have to perform the right click operations on the same element where our driver is currently then we just have to remove the WebElement argument from the method. Then, WebDriver will understand that it has to perform right click operations on the current or immediate WebElement. Hence, the method will look like below:

  • contextClick()

So the syntax will be like-

//Instance of Actions class

Actions act = new Actions(driver);

//Right click command, without argument

act.contextClick().build().perform();

Hope the core concept is very much clear now.

I am very much assured that you can handle the right click scenario when you find in your test automation requirement.

Well I was all about performing the right click operations in Selenium. I have written some more advanced Selenium operation, which I would like you to go through. Here are those:

How to perform drag and drop operations in Selenium WebDriver?

How to perform Mouse hover operations in Selenium WebDriver?

How to use Robot class in Selenium WebDriver?

Most important list of 20 exceptions in Selenium

How to handle these exceptions?

How to handle alerts in Selenium?

In Addition, I have an entire code as a sample on right click operations. You can try in your IDE:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
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.testng.annotations.Test;

public class RightClickOperations {
  @Test
  public void rightClickOperations() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");
	  
	  WebElement ele = driver.findElement(By.xpath("//*[@class='context-menu-one btn btn-neutral']"));
	  
	  WebElement ele2 = driver.findElement(By.xpath("//*[@class='context-menu-list context-menu-root']//li[7]"));
	  
	  //Instance of Actions class
	  Actions act = new Actions(driver);
	  
	  //Right click command
	  act.contextClick(ele).build().perform();
	  
	  Thread.sleep(3000);
	 
	  //Clicking on sub-menu
	  ele2.click();
	  
	  Alert alt = driver.switchTo().alert();
	  alt.accept();
	  
	  Thread.sleep(3000);
	  
	  driver.close();
	  
	  driver.quit();
  }
}

Leave a Reply