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.
This test performs a right-click (context click) on a specific element on a webpage and verifies that the context menu appears as expected.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | — | PASS |
| 2 | Navigates to URL https://swisnl.github.io/jQuery-contextMenu/demo.html | Page with a button labeled 'right click me' is loaded | — | PASS |
| 3 | Waits until the button with CSS selector 'span.context-menu-one.btn.btn-neutral' is clickable | Button is visible and enabled on the page | Button is clickable | PASS |
| 4 | Performs context click (right click) on the button using Actions class | Context menu is triggered to appear | — | PASS |
| 5 | Waits until the context menu with CSS selector 'ul.context-menu-list' is visible | Context menu is displayed on the page | Context menu is visible | PASS |
| 6 | Asserts that the context menu is displayed | Context menu is visible | assertTrue(contextMenu.isDisplayed()) | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
contextClick() method in Selenium WebDriver do?contextClick() method is designed to simulate a right-click action on a web element, which is different from a normal click.doubleClick(), dragAndDrop(), and sendKeys().Actions is instantiated with the WebDriver instance, e.g., new Actions(driver).new Actions(driver).contextClick(element).perform(); to execute the right-click.WebElement element = driver.findElement(By.id("menu"));
new Actions(driver).contextClick(element).perform();id="menu" correctly using findElement(By.id("menu")).contextClick(element).perform() chain performs a right-click on that element.WebElement button = driver.findElement(By.name("submit"));
Actions action = new Actions(driver);
action.contextClick().perform();contextClick() requires either no argument (to right-click at current mouse location) or a WebElement argument to specify where to right-click. Here, no argument is passed but no mouse position is set.contextClick(), so it will not right-click on the button.WebElement listItem = driver.findElement(By.cssSelector("ul#items li:first-child"));
WebElement deleteOption = driver.findElement(By.id("delete"));
// What next?contextClick(listItem).perform() to open the custom context menu on the list item.deleteOption.click(); as it is a normal click on a visible element.Pass the test and this pattern moves unknown