0
0
Selenium Javatesting~15 mins

Base test class pattern in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate login test using base test class pattern
Preconditions (3)
Step 1: Open browser and navigate to login page URL
Step 2: Enter 'testuser@example.com' in the email input field
Step 3: Enter 'Test@1234' in the password input field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page URL
✅ Expected Result: User successfully logs in and dashboard page is displayed
Automation Requirements - Selenium WebDriver with JUnit 5
Assertions Needed:
Verify current URL matches expected dashboard URL after login
Best Practices:
Use a base test class to initialize and quit WebDriver
Use @BeforeEach and @AfterEach annotations for setup and teardown
Use explicit waits to handle page load and element visibility
Use meaningful locators (By.id, By.name) instead of brittle XPath
Keep test methods focused and readable
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.assertEquals;

public class BaseTest {
    protected WebDriver driver;
    protected WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
    }

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

public class LoginTest extends BaseTest {
    private final String loginUrl = "https://example.com/login";
    private final String dashboardUrl = "https://example.com/dashboard";

    @Test
    public void testSuccessfulLogin() {
        driver.get(loginUrl);

        WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        emailInput.sendKeys("testuser@example.com");

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.sendKeys("Test@1234");

        WebElement loginButton = driver.findElement(By.id("loginButton"));
        loginButton.click();

        wait.until(ExpectedConditions.urlToBe(dashboardUrl));

        assertEquals(dashboardUrl, driver.getCurrentUrl(), "User should be redirected to dashboard after login");
    }
}

The BaseTest class sets up and tears down the WebDriver before and after each test. This avoids repeating setup code in every test class.

We use @BeforeEach to initialize the ChromeDriver and maximize the window. @AfterEach quits the driver to close the browser.

The LoginTest class extends BaseTest to reuse this setup. It navigates to the login page, waits for the email input to be visible, enters credentials, clicks login, and waits for the dashboard URL.

Assertions verify the final URL matches the expected dashboard URL, confirming successful login.

Explicit waits ensure elements are ready before interacting, preventing flaky tests.

Common Mistakes - 4 Pitfalls
Initializing WebDriver inside each test method instead of a base class
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not quitting WebDriver after tests
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials

Show Hint