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 |