0
0
Selenium Javatesting~10 mins

Context click (right click) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test performs a right-click (context click) on a specific element on a webpage and verifies that the context menu appears as expected.

Test Code - JUnit with Selenium WebDriver
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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;

public class ContextClickTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testContextClickShowsMenu() {
        driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html");

        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.context-menu-one.btn.btn-neutral")));

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

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

        assertTrue(contextMenu.isDisplayed(), "Context menu should be visible after right click");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to URL https://swisnl.github.io/jQuery-contextMenu/demo.htmlPage with a button labeled 'right click me' is loaded-PASS
3Waits until the button with CSS selector 'span.context-menu-one.btn.btn-neutral' is clickableButton is visible and enabled on the pageButton is clickablePASS
4Performs context click (right click) on the button using Actions classContext menu is triggered to appear-PASS
5Waits until the context menu with CSS selector 'ul.context-menu-list' is visibleContext menu is displayed on the pageContext menu is visiblePASS
6Asserts that the context menu is displayedContext menu is visibleassertTrue(contextMenu.isDisplayed())PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The context menu does not appear after right-clicking the button
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to perform a right-click on an element?
Aelement.click()
Bactions.doubleClick(element).perform()
Cactions.contextClick(element).perform()
Ddriver.rightClick(element)
Key Result
Always wait explicitly for elements to be clickable or visible before interacting or asserting to avoid flaky tests.