0
0
Selenium Javatesting~15 mins

Why selector mastery prevents fragile tests in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify login button functionality with robust selector
Preconditions (1)
Step 1: Locate the login button using a robust CSS selector based on id or unique attribute
Step 2: Click the login button
Step 3: Verify that the login form is submitted or the next page is loaded
✅ Expected Result: Login button is clicked successfully and the login process proceeds without errors
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the login button is clickable
Verify the page URL or title changes after clicking login button
Best Practices:
Use By.id or By.cssSelector with stable attributes for locating elements
Avoid using brittle XPath expressions that rely on element position
Use explicit waits to ensure elements are ready before interaction
Organize selectors in a Page Object Model for maintainability
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 LoginButtonTest {
    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 testLoginButtonClick() {
        // Use robust selector by id
        By loginButtonSelector = By.id("login-button");

        // Wait until login button is clickable
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(loginButtonSelector));

        // Click the login button
        loginButton.click();

        // Wait for URL to change or page title to indicate login success
        wait.until(ExpectedConditions.not(ExpectedConditions.urlToBe("https://example.com/login")));

        // Assert URL changed from login page
        String currentUrl = driver.getCurrentUrl();
        assertNotEquals("https://example.com/login", currentUrl, "URL should change after login button click");
    }

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

This test uses Selenium WebDriver with Java to automate clicking the login button on a login page.

We use By.id("login-button") as the selector because IDs are usually unique and stable, making the test less fragile.

We apply an explicit wait with ExpectedConditions.elementToBeClickable to ensure the button is ready before clicking.

After clicking, we wait until the URL changes from the login page URL, indicating the login process proceeded.

Finally, we assert that the URL is different from the login page URL to confirm the click had the expected effect.

Setup and teardown methods initialize and close the browser cleanly.

Common Mistakes - 3 Pitfalls
Using brittle XPath selectors that rely on element position like //div[3]/button
Not using explicit waits before interacting with elements
Hardcoding URLs or page titles without verifying dynamic changes
Bonus Challenge

Now add data-driven testing with 3 different login button selectors to verify robustness

Show Hint