#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Test Automation Frameworks

Setting Up Utils For Reusability

What Are Utility Classes?

Utility ("Utils") classes hold reusable static helper methods for common tasks — reading configuration, generating random test data, formatting dates, or building headers.

public class Utils {
    public static String randomEmail() {
        return "user" + System.currentTimeMillis() + "@test.com";
    }
}

Why This Matters

Utility classes prevent the same helper logic from being copy-pasted across dozens of test files, keeping the whole framework easier to maintain and extend.

Organising a utils Package

One giant Utils class quickly turns into a dumping ground. Split helpers by responsibility so each file stays small and easy to find:

ClassResponsibility
ConfigReaderReads URLs, browser names and timeouts from a properties file
WaitUtilsWraps explicit waits for visibility, clickability and page load
TestDataUtilsGenerates unique emails, names, phone numbers and dates
ScreenshotUtilsCaptures and saves screenshots on failure
JsonUtilsReads request bodies or expected results from JSON files

Building Two Helpers Step by Step

A configuration reader

public final class ConfigReader {
    private static final Properties PROPS = new Properties();

    static {
        try (InputStream in = ConfigReader.class
                .getClassLoader().getResourceAsStream("config.properties")) {
            if (in == null) {
                throw new IllegalStateException("config.properties not found on classpath");
            }
            PROPS.load(in);
        } catch (IOException e) {
            throw new IllegalStateException("Could not load config", e);
        }
    }

    private ConfigReader() { }   // no objects needed

    public static String get(String key) {
        String override = System.getProperty(key);   // allows -Dbrowser=firefox
        return override != null ? override : PROPS.getProperty(key);
    }
}

The file is loaded once from src/test/resources. Command-line overrides let CI choose a different browser or environment without editing the file.

A wait helper

public final class WaitUtils {
    private WaitUtils() { }

    public static WebElement waitForClickable(WebDriver driver, By locator) {
        int seconds = Integer.parseInt(ConfigReader.get("timeout"));
        return new WebDriverWait(driver, Duration.ofSeconds(seconds))
                .until(ExpectedConditions.elementToBeClickable(locator));
    }
}

A page object can now call WaitUtils.waitForClickable(driver, loginButton).click(); and the timeout lives in one config value.

Design Rules for Reliable Utilities

  • Keep helpers stateless. Pass the driver in as a parameter instead of storing it in a static field, which breaks parallel runs. If a shared driver is needed, hold it in a ThreadLocal.
  • No assertions inside utils. Helpers return values; tests decide what is correct.
  • Make data truly unique. A timestamp-based email can repeat when tests run in parallel. UUID.randomUUID() is safer.
  • Use a private constructor and final class for pure static helpers.
  • Avoid hard-coded paths such as C:\Users\...; use the classpath or paths relative to the project.

Review checklist: each helper has one clear job, a descriptive name, no hidden state, and is used in at least two places. If a helper is used once, it probably belongs in the page object or test instead.

Where Utils End and Other Layers Begin

A framework usually has three kinds of reusable code, and mixing them up is what makes utility classes bloated. Page objects know about one screen: its locators and the actions a user can take there. Utilities know nothing about any particular screen; they solve general problems such as waiting, reading files or creating data. Base test classes handle setup and teardown, such as starting the browser before each test and closing it afterwards.

A quick test for any new method: if it mentions a specific button, field or page, it belongs in a page object. If it would be useful in a completely different project, it belongs in utils. Keeping this line clear means new team members can guess where code lives, and changes to one screen never ripple into unrelated helpers. It also makes the utils package easy to copy into the next project you automate.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course