How to Use DataFakerUtils for Generating Random Test Data in anhtester/automationframeworkselenium

DataFakerUtils provides a static wrapper around the DataFaker library that centralizes locale-aware random data generation through reusable helper methods in DataGenerateUtils.

The anhtester/automationframeworkselenium repository includes a dedicated utility layer for creating realistic test data without manual instantiation. This article explains how to leverage DataFakerUtils and its companion class DataGenerateUtils to produce locale-specific names, addresses, and job titles for your Selenium test scripts.

Core Architecture

The framework separates concerns into two primary classes located in src/main/java/com/anhtester/utils/. DataFakerUtils manages the singleton Faker instance and locale configuration, while DataGenerateUtils exposes convenient static methods for common data types. This design ensures that all test classes share the same locale settings and eliminates repetitive Faker instantiation code.

DataFakerUtils: The Singleton Manager

In src/main/java/com/anhtester/utils/DataFakerUtils.java, the class maintains a single static Faker instance. The instance initializes lazily upon the first call to getFaker(), using the default locale defined in FrameworkConstants.LOCATE (set to en-US by default).

You can modify the behavior at runtime using two key methods:

  • setLocate(String locale) – Changes the locale for all subsequent data generation (e.g., "vi" for Vietnamese)
  • setFaker(Faker faker) – Replaces the entire underlying Faker object with a custom instance

DataGenerateUtils: The Helper Collection

The src/main/java/com/anhtester/utils/DataGenerateUtils.java file contains static helper methods that forward calls to the shared Faker instance. These methods provide type-safe access to common data patterns without exposing the underlying DataFaker API to your test scripts.

Available helpers include getFullName(), getPhoneNumber(), getFullAddress(), getJobTitle(), getCountry(), getZipCode(), getCityName(), getState(), getStreetName(), getKeySkill(), and getProgrammingLanguage().

Implementation Examples

Accessing the Raw Faker Instance

When you need data types not covered by the existing helpers, retrieve the underlying Faker object directly:

import com.anhtester.utils.DataFakerUtils;

Faker faker = DataFakerUtils.getFaker();

System.out.println(faker.name().fullName());          // e.g., "John Doe"
System.out.println(faker.internet().emailAddress()); // e.g., "john.doe@example.com"
System.out.println(faker.lorem().sentence());        // e.g., "The quick brown fox jumps over the lazy dog"

Using Static Helper Methods

For standard test data patterns, use the pre-built helpers from DataGenerateUtils:

import com.anhtester.utils.DataGenerateUtils;

String name = DataGenerateUtils.getFullName();               // "Emma Watson"
String phone = DataGenerateUtils.getPhoneNumber();          // "(555) 123-4567"
String address = DataGenerateUtils.getFullAddress();        // "123 Main St, San Francisco, CA 94107"
String jobTitle = DataGenerateUtils.getJobTitle();          // "Software Engineer"
String skill = DataGenerateUtils.getKeySkill();            // "Java"
String language = DataGenerateUtils.getProgrammingLanguage(); // "Python"

// Individual address components
String street = DataGenerateUtils.getStreetName();          // "Maple Avenue"
String city = DataGenerateUtils.getCityName();             // "San Francisco"
String state = DataGenerateUtils.getState();               // "California"
String country = DataGenerateUtils.getCountry();           // "United States"
String zipCode = DataGenerateUtils.getZipCode();           // "94107"

Changing Locale at Runtime

Switch the data generation locale dynamically to test internationalization scenarios:

// Switch to Vietnamese locale
DataFakerUtils.setLocate("vi");

// All subsequent calls return Vietnamese-formatted data
System.out.println(DataFakerUtils.getFaker().address().fullAddress());
// Output: "123 Đường Nguyễn Huệ, Quận 1, Thành phố Hồ Chí Minh"

// Revert to English
DataFakerUtils.setLocate("en-US");

Integration in Test Classes

The repository includes a demonstration in src/test/java/com/anhtester/projects/crm/testcases/TestSimpleCode.java. This test class showcases practical invocation patterns for the faker utilities within actual test methods:

public void testDataFaker() {
    // Print random address and job title using the shared instance
    System.out.println(DataFakerUtils.getFaker().address().fullAddress());
    System.out.println(DataFakerUtils.getFaker().job().title());
}

This pattern ensures that your test methods remain clean and focused on assertions rather than data setup logic. Since DataFakerUtils manages the Faker lifecycle, you avoid memory overhead from creating multiple instances in large test suites.

Summary

  • Centralized locale management: Change the FrameworkConstants.LOCATE value or call setLocate() once to affect all generated data across the test suite.
  • Reusable static helpers: DataGenerateUtils eliminates boilerplate code by exposing common data types as simple method calls.
  • Extensible design: Add new helper methods to DataGenerateUtils as your domain requires additional specific data patterns.
  • Zero configuration: The Faker instance initializes automatically on first use with sensible defaults.

Frequently Asked Questions

How do I change the locale for generated test data?

Call DataFakerUtils.setLocate(String locale) with a valid locale string such as "en-US", "vi", or "de". This updates the static Faker instance immediately, causing all subsequent calls to getFaker() or DataGenerateUtils helpers to return data formatted for that region.

Can I add custom helper methods to DataGenerateUtils?

Yes. Open src/main/java/com/anhtester/utils/DataGenerateUtils.java and add new public static methods that delegate to DataFakerUtils.getFaker(). For example, you could create getCreditCardNumber() that returns DataFakerUtils.getFaker().finance().creditCard().

Is DataFakerUtils thread-safe for parallel test execution?

The static Faker instance held by DataFakerUtils is shared across threads. While the underlying DataFaker library is generally thread-safe for read operations, concurrent modifications to the locale via setLocate() during parallel execution may cause race conditions. Initialize your locale in a @BeforeSuite or @BeforeClass setup method to ensure stable configuration before tests run concurrently.

How do I generate data types not covered by existing helpers?

Retrieve the raw Faker instance using DataFakerUtils.getFaker() and access any DataFaker provider directly. For example, DataFakerUtils.getFaker().animal().name() generates random animal names, or DataFakerUtils.getFaker().book().title() produces book titles without requiring a specific helper method in DataGenerateUtils.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →