Selenium WebDriver – How To Perform Right Click

In some web pages, function menus is not directly shown on the page. Sometimes you need to right click a button or something else to make the menu list show up, then you can click the menu item in the popup list. Test this kind of function is easy in manual test, but how to test it in selenium webdriver automation test? In this article we will use two examples to show you how to use selenium webdriver to perform right click testing automatically in java code.

All examples in this article will use WebDriver’s Actions class to perform the action as below code.

/* First build the webdriver contextClick action object. */
Actions rightClickAction = new Actions(ffDriver).contextClick(menuButtonElement);
/* Use the context action object above to perform right click action. */
rightClickAction.build().perform();

Example 1

This example only take effective in Firefox browser.

Steps In Selenium Test Scripts:

  1. Open Firefox browser and browse following url page.
    http://the-internet.herokuapp.com/context_menu
  2. Right click a div web element.
  3. Move the arrow key in the popup context menu.
  4. Click the menu item we want.
  5. Get the text in the popup alert dialog.
private static WebDriver ffDriver = null;
 
 @BeforeClass
 /* This method is used to initiate the Firefox Driver object. */
 public static void Setup() {
 if(ffDriver==null)
 {
 ffDriver = new FirefoxDriver();
 /* Maximize the Firefox browser window. */
 ffDriver.manage().window().maximize();
 }
 }
 @Test
 /* This method is used to test right click action in Firefox browser. */
 public void testRightClickContextMenuInFirefox() throws InterruptedException
 {
 /* First goto a page which has a javascript implemented menu. */
 this.ffDriver.get("http://the-internet.herokuapp.com/context_menu");
 
 /* Wait for 3 seconds for the page load complete. */
 Thread.sleep(3000);
 
 /* Find the web element. */
 By byId = By.id("hot-spot");
 WebElement rcWebElement = this.ffDriver.findElement(byId);
 
 /* Build the webdriver contextClick action object. */
 Actions rightClickAction = new Actions(ffDriver).contextClick(rcWebElement);
 
 /* Right click the web element and show up the context menu. */
 rightClickAction.build().perform();
/* Move the arrow key in the popup menu. */
 for(int i=0;i<5;i++)
 {
 rcWebElement.sendKeys(Keys.ARROW_DOWN);
 Thread.sleep(1000);
 }
 
 /* Click enter on the menu item. */
 rcWebElement.sendKeys(Keys.ENTER);
 
 Thread.sleep(1000);
 
 /* Get the alert object for the popup alert. */
 Alert rcAlert = ffDriver.switchTo().alert();
 
 String rcAlertText = rcAlert.getText();
 
 Assert.assertEquals("Click 'the-internet' menu item failed.", rcAlertText, "You selected a context menu");
 
 rcAlert.accept();
 
 }
@AfterClass
 /* This method is used to quit the webdriver object 
 * and close web browser after all the test runs. */
 public static void quitBrowser() {
 if(ffDriver != null)
 {
 /* Quit and close Firefox browser. */
 ffDriver.quit();
 }
 }

Example 2

This example can be run under any popular web browser.

Steps In Selenium Test Scripts:

  1. Go to page http://swisnl.github.io/jQuery-contextMenu/demo.html
  2. Right click “right click me” css button.
  3. Move the arrow key to the Copy menu item.
  4. Click Copy in the popup menu list.
  5. Get the alert text in the popup alert.

The selenium test java code for example 2 is very similar with the java code for example 1. The only difference is the web page url and the web element finding method. This example use XPath to find the css button.

 /* First goto a page which has a javascript implemented menu. */
 this.ffDriver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");
 
 /* Wait for 3 seconds for the page load complete. */
 Thread.sleep(3000);
 
 /* Find the css button which can trigger right click action. */
 By byXPath = By.xpath("//span[@class='context-menu-one btn btn-neutral']");
 WebElement rcWebElement = this.ffDriver.findElement(byXPath);

You can download the attached file to see the full example codes.

[download id=”821″]

1 thought on “Selenium WebDriver – How To Perform Right Click”

  1. If you want to right click on any element then selenium doesn’t provide any direct method for that. We need to use Actions class provided by WebDriver. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including right click, double click, drag and drop etc.
    Here is the code:-

    Actions action = new Actions(driver);
    WebElement element = driver.findElement(By.id(“elementId”));
    action.contextClick(element).perform();

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.