0
0
Selenium Javatesting~15 mins

Why reliable element location ensures stability in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify login button is clickable using reliable locator
Preconditions (1)
Step 1: Locate the login button using a reliable locator (e.g., By.id 'loginBtn')
Step 2: Click the login button
Step 3: Verify that the next page URL contains '/dashboard'
✅ Expected Result: The login button is found and clicked successfully, and the user is navigated to the dashboard page
Automation Requirements - Selenium WebDriver with Java and JUnit 5
Assertions Needed:
Assert that the login button is displayed and enabled before clicking
Assert that the URL after clicking contains '/dashboard'
Best Practices:
Use By.id or By.name locators instead of brittle XPath or CSS selectors
Use explicit waits to wait for elements to be clickable
Use Page Object Model to separate locators and actions
Avoid hardcoded waits like Thread.sleep
Automated Solution
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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 java.time.Duration;

import static org.junit.jupiter.api.Assertions.*;

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 testLoginButtonClickable() {
        // Locate login button by reliable locator (id)
        By loginButtonLocator = By.id("loginBtn");

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

        // Assert button is displayed and enabled
        assertTrue(loginButton.isDisplayed(), "Login button should be visible");
        assertTrue(loginButton.isEnabled(), "Login button should be enabled");

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

        // Wait until URL contains '/dashboard'
        boolean urlContainsDashboard = wait.until(ExpectedConditions.urlContains("/dashboard"));

        // Assert navigation to dashboard
        assertTrue(urlContainsDashboard, "URL should contain '/dashboard' after login");
    }

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

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

Setup: We open the Chrome browser and navigate to the login page URL.

Locating element: We use By.id("loginBtn") which is a reliable locator because IDs are unique and less likely to change.

Waiting: We use explicit wait ExpectedConditions.elementToBeClickable to wait until the login button is ready to be clicked. This avoids flaky tests caused by timing issues.

Assertions: We check the button is visible and enabled before clicking. After clicking, we wait and assert that the URL contains '/dashboard' to confirm navigation.

Teardown: We close the browser after the test to clean up.

This approach ensures stability because it uses reliable locators, explicit waits, and clear assertions, reducing failures caused by page load delays or locator changes.

Common Mistakes - 4 Pitfalls
Using XPath with absolute paths like /html/body/div[2]/button
Using Thread.sleep() to wait for elements
Not checking if element is visible or enabled before clicking
Hardcoding URLs or text without verification
Bonus Challenge

Now add data-driven testing with 3 different login button IDs to verify stability across variations.

Show Hint