Skip to main content

How to Right-click

Intro​

Sometimes you'll run into an app that has functionality hidden behind a right-click menu (a.k.a. a context menu). These menus tend to be system level menus that are untouchable by Selenium. So how do you test this functionality?

A Solution​

By leveraging Selenium's Actions API we can issue a right-click command (a.k.a. a context click) on the element, which triggers the code the page runs when its context menu is opened.

Keep in mind that Selenium can't interact with the menu the browser itself displays (e.g., to select one of its options), since it's part of the browser and not of the page. But most custom context menus are rendered by the page, and those you can work with like any other element.

Example​

Let's use an example from the-internet that runs some JavaScript when we right-click on a specific area of the page: it triggers a JavaScript alert with the text You selected a context menu.

After visiting the page we find the area (with the id hot-spot) and right-click it with the Actions API. Then we wait for the alert, check its text, and accept it.

Expected Behavior​

  • Open the browser and visit the page
  • Find and right-click the area which has a custom context menu action
  • JavaScript alert appears
  • Grab the text of the JavaScript alert
  • Assert that the text from the alert is what we expect
  • Close the browser

Summary​

Hopefully this tip has helped you test functionality that is hidden behind a right-click.

Happy Testing!

Special Thanks​

Roman Isko
Contributed the initial Java code for this tip.

Authors​

Dave Piacente
Original creator of Elemental Selenium
Diego Molina
Elemental Selenium maintainer