Test Overview
This test opens a browser, navigates to a sample page, finds a button using an XPath locator, clicks it, and verifies the expected text appears.
This test opens a browser, navigates to a sample page, finds a button using an XPath locator, 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 FindElementByXPathTest { private WebDriver driver; @BeforeEach public void setUp() { // Set the path to chromedriver executable if necessary // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testFindElementByXPath() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.xpath("//button[@id='submit-btn']")); button.click(); WebElement message = driver.findElement(By.xpath("//div[@id='message']")); assertEquals("Success", 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 https://example.com/testpage | Page loads with a button having id 'submit-btn' and a hidden message div | - | PASS |
| 3 | Finds button element using XPath //button[@id='submit-btn'] | Button element located on the page | Element is found and not null | PASS |
| 4 | Clicks the button element | Button click triggers message display | - | PASS |
| 5 | Finds message element using XPath //div[@id='message'] | Message element is visible with text 'Success' | Element text equals 'Success' | PASS |
| 6 | Test ends and browser closes | Browser window closed | - | PASS |