Test Overview
This test opens a webpage and clicks a button using a reliable locator. It verifies the button click changes the page content as expected, showing how stable element location helps keep tests reliable.
This test opens a webpage and clicks a button using a reliable locator. It verifies the button click changes the page content as expected, showing how stable element location helps keep tests reliable.
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; import java.time.Duration; public class ReliableLocatorTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testClickButtonWithReliableLocator() { driver.get("https://example.com/testpage"); // Use reliable locator by id WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit-button"))); button.click(); // Verify the page content changed after click WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result-message"))); String actualText = message.getText(); assertEquals("Success!", actualText); } @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, ready to navigate | — | PASS |
| 2 | Navigates to https://example.com/testpage | Page loads with a button having id 'submit-button' and hidden result message | — | PASS |
| 3 | Waits until button with id 'submit-button' is clickable and finds it | Button is visible and enabled on the page | Element located by id 'submit-button' is present and clickable | PASS |
| 4 | Clicks the button | Button click triggers page update | — | PASS |
| 5 | Waits until element with id 'result-message' is visible | Result message appears on the page with text 'Success!' | Element located by id 'result-message' is visible | PASS |
| 6 | Checks that the result message text equals 'Success!' | Text content of result message is 'Success!' | Assert actual text equals expected text 'Success!' | PASS |
| 7 | Test ends and browser closes | Browser window closed | — | PASS |
id or name in Selenium tests?id or name rarely change, so tests find elements easily.id in Selenium Java?By.id("value") with parentheses and quotes.driver.findElement(By.id("username")); others have syntax errors or wrong usage.WebElement button = driver.findElement(By.id("submitBtn"));
button.click();id='submitBtn' is not present on the page?findElement cannot find the element, it throws NoSuchElementException.WebElement element = driver.findElement(By.xpath("//div[@class='btn']"));
element.click();//div[@class='btn'] may match multiple elements or change if classes change, causing instability.id attribute is unique per page and less likely to change, making it the most stable locator.Pass the test and this pattern moves unknown