0
0
Selenium Javatesting~15 mins

XPath functions (contains, starts-with) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login button using XPath functions contains and starts-with
Preconditions (1)
Step 1: Locate the login button using XPath with contains() function on the button's class attribute containing 'btn-login'
Step 2: Verify the login button is displayed
Step 3: Locate the login button using XPath with starts-with() function on the button's id attribute starting with 'loginBtn'
Step 4: Verify the login button is enabled
✅ Expected Result: The login button is found using both XPath functions and is displayed and enabled
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the login button located by contains() XPath is displayed
Assert that the login button located by starts-with() XPath is enabled
Best Practices:
Use explicit waits to wait for the login button to be present and visible
Use By.xpath locator with correct XPath syntax
Use meaningful variable names
Close the browser after test execution
Automated Solution
Selenium Java
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.*;
import java.time.Duration;

public class LoginButtonXPathTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/login");
    }

    @Test
    public void testLoginButtonUsingXPathFunctions() {
        // Locate login button using contains() on class attribute
        By loginBtnContains = By.xpath("//button[contains(@class, 'btn-login')]");
        WebElement buttonContains = wait.until(ExpectedConditions.visibilityOfElementLocated(loginBtnContains));
        assertTrue(buttonContains.isDisplayed(), "Login button located by contains() should be displayed");

        // Locate login button using starts-with() on id attribute
        By loginBtnStartsWith = By.xpath("//button[starts-with(@id, 'loginBtn')]");
        WebElement buttonStartsWith = wait.until(ExpectedConditions.elementToBeClickable(loginBtnStartsWith));
        assertTrue(buttonStartsWith.isEnabled(), "Login button located by starts-with() should be enabled");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test uses Selenium WebDriver with Java to automate the manual test case.

In setUp(), we open the Chrome browser and navigate to the login page.

In the test method, we use By.xpath with contains() to find the login button by part of its class attribute. We wait explicitly until it is visible, then assert it is displayed.

Next, we use starts-with() to find the login button by the start of its id attribute. We wait until it is clickable, then assert it is enabled.

Finally, in tearDown(), we close the browser to clean up.

This approach uses explicit waits to avoid timing issues and uses clear assertions to verify the button's presence and state.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using absolute XPath instead of relative XPath with functions
Not verifying element visibility or enabled state before interacting
Bonus Challenge

Now add data-driven testing with 3 different button class name fragments to locate the login button using contains()

Show Hint