Test Overview
This test opens a web page and uses XPath functions contains and starts-with to find elements by partial attribute values. It verifies that the correct elements are found and clickable.
This test opens a web page and uses XPath functions contains and starts-with to find elements by partial attribute values. It verifies that the correct elements are found and clickable.
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.assertTrue; import java.time.Duration; public class XPathFunctionsTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testContainsAndStartsWithXPath() { driver.get("https://example.com/testpage"); // Find element with id containing 'submit' WebElement submitButton = wait.until( ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@id, 'submit')]") ) ); assertTrue(submitButton.isDisplayed()); // Find element with class starting with 'nav-' WebElement navMenu = wait.until( ExpectedConditions.presenceOfElementLocated(By.xpath("//nav[starts-with(@class, 'nav-')]") ) ); assertTrue(navMenu.isDisplayed()); // Click the submit button submitButton.click(); } @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 | Navigate to https://example.com/testpage | Browser loads the test page with buttons and navigation menu | - | PASS |
| 3 | Wait until button with id containing 'submit' is present using XPath contains() | Button element with id like 'btn-submit' is found in DOM | Check that submit button is displayed | PASS |
| 4 | Wait until nav element with class starting with 'nav-' is present using XPath starts-with() | Navigation element with class like 'nav-main' is found in DOM | Check that navigation menu is displayed | PASS |
| 5 | Click the submit button | Submit button is clicked, triggering form submission or action | - | PASS |
| 6 | Test ends and browser closes | Browser window closes cleanly | - | PASS |