0
0
Selenium Javatesting~15 mins

Utility classes in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate login using utility class for WebDriver setup
Preconditions (3)
Step 1: Use the utility class to initialize the WebDriver
Step 2: Navigate to the login page URL
Step 3: Enter 'testuser@example.com' in the email input field with id 'email'
Step 4: Enter 'Test@1234' in the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the dashboard page loads and URL contains '/dashboard'
✅ Expected Result: User is successfully logged in and dashboard page is displayed with URL containing '/dashboard'
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Best Practices:
Use a utility class to initialize and manage WebDriver instance
Use explicit waits to wait for page load or element presence
Use meaningful locators (id) for elements
Use TestNG assertions for validation
Automated Solution
Selenium Java
package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utils.DriverUtil;

import java.time.Duration;

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

    @BeforeClass
    public void setUp() {
        driver = DriverUtil.getDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testValidLogin() {
        driver.get("https://example.com/login");

        driver.findElement(By.id("email")).sendKeys("testuser@example.com");
        driver.findElement(By.id("password")).sendKeys("Test@1234");
        driver.findElement(By.id("loginBtn")).click();

        wait.until(ExpectedConditions.urlContains("/dashboard"));

        String currentUrl = driver.getCurrentUrl();
        Assert.assertTrue(currentUrl.contains("/dashboard"), "URL should contain '/dashboard' after login");
    }

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

// utils/DriverUtil.java
package utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverUtil {
    private static WebDriver driver;

    private DriverUtil() {
        // private constructor to prevent instantiation
    }

    public static WebDriver getDriver() {
        if (driver == null) {
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
        return driver;
    }

    public static void quitDriver() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }
}

The test class LoginTest uses the utility class DriverUtil to get the WebDriver instance. This avoids repeating WebDriver setup code in every test.

In @BeforeClass, we initialize the driver and explicit wait. The test navigates to the login page, enters credentials, clicks login, and waits until the URL contains '/dashboard'.

We assert that the URL contains '/dashboard' to confirm successful login.

In @AfterClass, we quit the driver to close the browser.

The utility class DriverUtil manages a single WebDriver instance with lazy initialization and maximizes the window. It also provides a method to quit the driver and clean up.

This structure keeps test code clean, reusable, and easy to maintain.

Common Mistakes - 3 Pitfalls
Initializing WebDriver directly in each test without a utility class
Using Thread.sleep() instead of explicit waits
Using brittle locators like absolute XPath
Bonus Challenge

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

Show Hint