0
0
Selenium Javatesting~15 mins

Context click (right click) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify context click opens context menu
Preconditions (1)
Step 1: Locate the element with id 'context-menu-area'
Step 2: Perform a right click (context click) on the element
Step 3: Verify that the context menu with id 'context-menu' becomes visible
✅ Expected Result: The context menu appears after right clicking on the element
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the context menu is displayed after right click
Best Practices:
Use Actions class for context click
Use explicit waits to wait for context menu visibility
Use By.id locator for elements
Close WebDriver properly after test
Automated Solution
Selenium Java
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;
import java.time.Duration;

public class ContextClickTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/context-menu-page");

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

            WebElement contextArea = wait.until(ExpectedConditions.elementToBeClickable(By.id("context-menu-area")));

            Actions actions = new Actions(driver);
            actions.contextClick(contextArea).perform();

            WebElement contextMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("context-menu")));

            if (contextMenu.isDisplayed()) {
                System.out.println("Test Passed: Context menu is visible after right click.");
            } else {
                System.out.println("Test Failed: Context menu is not visible after right click.");
            }
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate the manual test case.

First, it opens the target web page.

Then it waits explicitly for the element with id 'context-menu-area' to be clickable.

It uses the Actions class to perform a context click (right click) on that element.

After the right click, it waits explicitly for the context menu with id 'context-menu' to become visible.

Finally, it asserts that the context menu is displayed and prints the test result.

The driver quits in a finally block to ensure the browser closes even if an error occurs.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator like XPath with absolute paths
Not using Actions class for context click
Not closing the WebDriver after test
Bonus Challenge

Now add data-driven testing with 3 different elements having context menus to verify right click works on all.

Show Hint