</>

Technology

Cucumber

Difficulty

Advanced

Interview Question

How do you manage environment-specific configuration in Cucumber?

Answer

Environment-Specific Configuration in Cucumber

Project Structure

CODE
src/
  test/
    resources/
      config/
        config.dev.properties
        config.staging.properties
        config.prod.properties
      features/
        ...

config.staging.properties

PROPERTIES
base.url=https://staging.example.com
api.base.url=https://api.staging.example.com
browser=chrome
timeout=15
db.url=jdbc:postgresql://staging-db:5432/testdb
db.user=testuser
db.password=stagingpass

ConfigReader Utility

Java
package utils;

import java.io.*;
import java.util.Properties;

public class ConfigReader {

    private static Properties properties;

    static {
        String env = System.getProperty("env", "staging");  // default: staging
        String configFile = "src/test/resources/config/config." + env + ".properties";

        try (FileInputStream fis = new FileInputStream(configFile)) {
            properties = new Properties();
            properties.load(fis);
        } catch (IOException e) {
            throw new RuntimeException("Cannot load config: " + configFile, e);
        }
    }

    public static String get(String key) {
        String value = System.getProperty(key);  // CLI override takes priority
        return value != null ? value : properties.getProperty(key);
    }

    public static String getBaseUrl()    { return get("base.url"); }
    public static String getBrowser()    { return get("browser"); }
    public static int    getTimeout()    { return Integer.parseInt(get("timeout")); }
}

Using ConfigReader in Hooks

Java
@Before
public void setUp() {
    String browser = ConfigReader.getBrowser();

    WebDriverManager.getInstance(
        browser.equalsIgnoreCase("firefox") ? FirefoxDriver.class : ChromeDriver.class
    ).setup();

    ctx.driver = browser.equalsIgnoreCase("firefox")
        ? new FirefoxDriver()
        : new ChromeDriver();

    ctx.driver.get(ConfigReader.getBaseUrl());
}

Maven Run Commands

Bash
# Run against staging
mvn test -Denv=staging

# Run against dev
mvn test -Denv=dev -Dcucumber.filter.tags="@smoke"

# Override specific property
mvn test -Denv=staging -Dbase.url=https://my-feature-branch.example.com

# CI/CD pipeline
mvn test -Denv=${ENVIRONMENT} -Dcucumber.filter.tags="@smoke"

Jenkins Build Parameters

GROOVY
parameters {
    choice(name: 'ENVIRONMENT', choices: ['staging', 'dev', 'prod'], description: 'Target environment')
    choice(name: 'TAGS', choices: ['@smoke', '@regression', '@sanity'], description: 'Test suite to run')
}

stages {
    stage('Run Tests') {
        steps {
            sh "mvn clean test -Denv=${params.ENVIRONMENT} -Dcucumber.filter.tags='${params.TAGS}'"
        }
    }
}

Multiple Environment Profiles in pom.xml

XML
<profiles>
    <profile>
        <id>staging</id>
        <properties>
            <env>staging</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>
Bash
mvn test -Pstaging
mvn test -Pprod

Follow AutomateQA

Related Topics