Test Overview
This test opens a web page, finds a button by its ID, clicks it, and verifies that a confirmation message appears.
This test opens a web page, finds a button by its ID, clicks it, and verifies that a confirmation message appears.
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.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.assertEquals; public class ClickActionTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, 10); } @Test public void testButtonClickShowsMessage() { driver.get("https://example.com/click-test"); WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("click-button"))); button.click(); WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmation-message"))); assertEquals("Button clicked!", message.getText()); } @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://example.com/click-test | Page loads with a button having id 'click-button' | — | PASS |
| 3 | Waits until button with id 'click-button' is clickable and finds it | Button is visible and enabled for clicking | Checks button is clickable | PASS |
| 4 | Clicks the button | Button is clicked, triggering page action | — | PASS |
| 5 | Waits until confirmation message with id 'confirmation-message' is visible | Confirmation message appears on the page | Checks message is visible | PASS |
| 6 | Verifies the confirmation message text equals 'Button clicked!' | Message text is displayed as expected | assertEquals("Button clicked!", message.getText()) | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
click() method do in Selenium WebDriver?click()click() method is designed to simulate a user clicking on a web element like a button or link.sendKeys(), navigation uses get(), and finding elements uses findElement(). None of these perform clicks.findElement(By.id())driver.findElement(By.id("submitBtn")).click() on the found elementclick() on the WebElement to simulate the click.driver.findElement(By.id("loginBtn")).click();ElementNotVisibleException or ElementNotInteractableException, not NoSuchElementException.WebElement button = driver.findElement(By.name("submit"));
button.click();ElementNotInteractableException.By.name("submit") finds the element, so no error here.Actions class can move the mouse to the menu element, triggering the button to appear, then click it.Pass the test and this pattern moves unknown