Utility classes in Selenium Java - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of login credentials
Practice
Solution
Step 1: Understand utility class role
Utility classes contain static methods to reuse code easily across tests.Step 2: Compare options with utility class purpose
Only To provide reusable static methods for common tasks describes reusable static methods, which is the utility class purpose.Final Answer:
To provide reusable static methods for common tasks -> Option DQuick Check:
Utility class = reusable static methods [OK]
- Confusing utility class with test data storage
- Thinking utility class creates WebDriver instances
- Mixing utility class with test step definitions
Solution
Step 1: Recall utility method syntax
Utility methods are static so they can be called without creating an object.Step 2: Check each option's syntax
public static void openBrowser() {} correctly declares a public static method with return type void.Final Answer:
public static void openBrowser() {} -> Option AQuick Check:
Utility method = public static method [OK]
- Omitting static keyword
- Missing return type
- Incorrect method declaration syntax
public static String getPageTitle(WebDriver driver) {
return driver.getTitle();
}What will be the output if the current page title is "Home Page"?
Solution
Step 1: Understand method behavior
The method returns the page title by calling driver.getTitle().Step 2: Check given page title
The page title is "Home Page", so the method returns this string.Final Answer:
"Home Page" -> Option CQuick Check:
Method returns page title string [OK]
- Confusing method return with method name
- Expecting null instead of actual title
- Assuming syntax error without checking code
public static void clickElement(WebElement element) {
element.click;
}Solution
Step 1: Check method syntax
The method calls element.click but misses parentheses, which is invalid syntax.Step 2: Validate other options
Passing WebElement is valid, return type void is fine, static allowed in utility methods.Final Answer:
Missing parentheses after click method -> Option AQuick Check:
Method call needs () after method name [OK]
- Forgetting parentheses on method calls
- Thinking WebElement can't be parameter
- Assuming static keyword causes error
Solution
Step 1: Understand utility method purpose
Utility methods should provide reusable, clean code for common tasks like waiting and clicking.Step 2: Evaluate options for best practice
Create a static method that uses WebDriverWait and then clicks the element uses static method with WebDriverWait, which is efficient and reusable. Others are less reliable or not reusable.Final Answer:
Create a static method that uses WebDriverWait and then clicks the element -> Option BQuick Check:
Use static method with explicit wait [OK]
- Using Thread.sleep() causing slow tests
- Not using static method for utility
- Putting wait logic inside tests, not utility
Ready to prove it?
Pass the test and this pattern moves unknown
