Test Overview
This test opens a web page, finds a button using XPath with an attribute filter, clicks it, and verifies the expected text appears.
This test opens a web page, finds a button using XPath with an attribute filter, clicks it, and verifies the expected text appears.
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 XPathAttributeTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testButtonClickByXPathAttribute() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.xpath("//button[@id='submit-btn']")); button.click(); WebElement message = driver.findElement(By.id("result-message")); assertEquals("Success", message.getText()); } @AfterEach public void tearDown() { 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 https://example.com/testpage | Page loads with a button having id 'submit-btn' and a hidden result message | - | PASS |
| 3 | Finds button using XPath //button[@id='submit-btn'] | Button element located successfully | Element found matches XPath with attribute id='submit-btn' | PASS |
| 4 | Clicks the button | Button click triggers page update showing result message | - | PASS |
| 5 | Finds element with id 'result-message' | Result message element is visible with text 'Success' | Element text equals 'Success' | PASS |
| 6 | Test ends and browser closes | Browser window closed | - | PASS |