</>

Technology

Cucumber

Difficulty

Advanced

Interview Question

How do you handle dynamic test data (random emails, unique IDs) in Cucumber?

Answer

Dynamic Test Data in Cucumber

The Problem

Gherkin
-- This fails on second run because email already exists:
When the user registers with email "test@example.com"

Solution 1: Faker Library for Realistic Random Data

XML
<dependency>
    <groupId>net.datafaker</groupId>
    <artifactId>datafaker</artifactId>
    <version>2.1.0</version>
</dependency>
Java
import net.datafaker.Faker;

public class TestDataGenerator {
    private static final Faker faker = new Faker();

    public static String randomEmail() {
        return faker.internet().emailAddress();
        // → "john.smith.7g3k@example.org"
    }

    public static String randomName() {
        return faker.name().fullName();
        // → "Jane Williams"
    }

    public static String randomPhone() {
        return faker.phoneNumber().cellPhone();
        // → "9876543210"
    }

    public static String uniqueEmail(String prefix) {
        return prefix + System.currentTimeMillis() + "@test.com";
        // → "qa1718123456789@test.com"
    }

    public static String randomPassword() {
        return "Test@" + faker.number().digits(6);
        // → "Test@847291"
    }
}

Solution 2: Store Dynamic Data in ScenarioContext

Java
// ScenarioContext.java
public class ScenarioContext {
    public WebDriver driver;
    public String registeredEmail;
    public String generatedOrderId;
    public String createdUserId;
}

// Step definitions
public class RegistrationSteps {
    private final ScenarioContext ctx;
    public RegistrationSteps(ScenarioContext ctx) { this.ctx = ctx; }

    @When("the user registers with a unique email")
    public void registerWithUniqueEmail() {
        ctx.registeredEmail = TestDataGenerator.uniqueEmail("qa");
        registrationPage.enterEmail(ctx.registeredEmail);
        registrationPage.submit();
    }

    @Then("the confirmation email should be sent to the registered address")
    public void verifyConfirmationEmail() {
        // Use ctx.registeredEmail — the same unique value
        emailClient.waitForEmail(ctx.registeredEmail, "Registration Confirmation");
    }
}

Solution 3: Feature File with Placeholder Indication

Gherkin
Scenario: Register with unique credentials
  When the user registers with a unique email address
  And the user completes registration
  Then the welcome email should be sent to the registered email

-- NOT:
-- When the user registers with email "fixed@test.com"

Solution 4: Scenario Outline with Multiple Pre-set Data Points

Gherkin
Scenario Outline: Register different user types
  When the user registers as "<role>" with email "<email>"
  Then the user should be assigned the "<role>" permissions

  Examples:
    | role    | email                         |
    | ADMIN   | admin.test@qa-team.com        |
    | MANAGER | manager.test@qa-team.com      |
    | USER    | user.test@qa-team.com         |

Use dedicated test email domains that your team controls.

Solution 5: API-Based Test Data Setup

Java
@Before("@needs-user")
public void createTestUserViaApi() {
    String uniqueEmail = TestDataGenerator.uniqueEmail("api");
    Response resp = given()
        .body(Map.of("email", uniqueEmail, "role", "USER"))
        .post("/api/users");

    ctx.createdUserId = resp.jsonPath().getString("id");
    ctx.registeredEmail = uniqueEmail;
}

@After("@needs-user")
public void deleteTestUserViaApi() {
    if (ctx.createdUserId != null) {
        delete("/api/users/" + ctx.createdUserId);
    }
}

Follow AutomateQA

Related Topics