Test Overview
This test opens a web page, finds a button using XPath that matches its visible text, clicks it, and verifies the expected result appears on the page.
This test opens a web page, finds a button using XPath that matches its visible text, clicks it, and verifies the expected result appears on the page.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; 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 XPathTextTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testClickButtonByText() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.xpath("//button[text()='Click Me']")); button.click(); WebElement message = driver.findElement(By.id("result-message")); assertEquals("Button clicked!", message.getText()); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test framework initializes ChromeDriver | - | PASS |
| 2 | Browser opens | Chrome browser window opens | - | PASS |
| 3 | Navigates to https://example.com/testpage | Page loads with a button labeled 'Click Me' | Page title or URL can be checked (not shown here) | PASS |
| 4 | Finds button element using XPath //button[text()='Click Me'] | Button element with exact text 'Click Me' is located | Element is found and not null | PASS |
| 5 | Clicks the button | Button click triggers page update | - | PASS |
| 6 | Finds element with id 'result-message' | Element showing result message is present | Element is found and not null | PASS |
| 7 | Checks that the text of 'result-message' is 'Button clicked!' | Text content matches expected string | assertEquals("Button clicked!", message.getText()) | PASS |
| 8 | Test ends and browser closes | Browser window closes, driver quits | - | PASS |