Monday, December 8, 2025

thumbnail

Reading Environment Variables in Selenium Tests

 Reading environment variables in Selenium tests is a best practice for keeping sensitive or environment-specific values out of code—such as URLs, credentials, API keys, and browser configurations.


Below is a clean, practical guide showing how to read environment variables in Java Selenium tests, using multiple approaches (Java, Maven, TestNG).


✅ 1. Using System.getenv() (Most Common & Recommended)


Java provides a simple way to read environment variables:


String baseUrl = System.getenv("BASE_URL");

String username = System.getenv("USERNAME");

String password = System.getenv("PASSWORD");


✓ Usage Example in Selenium

@Test

public void openSite() {

    String baseUrl = System.getenv("BASE_URL");

    WebDriver driver = new ChromeDriver();


    driver.get(baseUrl);

    driver.quit();

}


๐Ÿ“Œ IMPORTANT


System.getenv() reads OS-level environment variables.


You must define them in the OS, CI/CD pipeline, or terminal.


Linux/Mac


export BASE_URL=https://qa.example.com



Windows (PowerShell)


setx BASE_URL "https://qa.example.com"


✅ 2. Using Maven Command Line (-D System Properties)


Often used in CI or when switching environments easily.


Run:

mvn test -DbaseUrl=https://qa.example.com -Dbrowser=chrome


Read in Selenium Tests:

String baseUrl = System.getProperty("baseUrl");

String browser  = System.getProperty("browser");


Example:

@Test

public void testEnv() {

    System.out.println("Running on: " + System.getProperty("baseUrl"));

}


✔ Best for:


Jenkins, GitHub Actions, Azure Pipeline


Multiple environments (DEV/QA/PROD)


✅ 3. Reading Environment Variables in TestNG With @Parameters


Useful when passing values from testng.xml.


testng.xml

<test name="QA Test">

    <parameter name="baseUrl" value="${env.BASE_URL}" />

    <classes>

        <class name="tests.MyTest"/>

    </classes>

</test>


Java Code

@Test

@Parameters("baseUrl")

public void runTest(String baseUrl) {

    System.out.println("URL: " + baseUrl);

}


Note:


Requires a plugin (Maven Surefire) to enable ${env.VAR}.


✅ 4. Using Java Utility Class (Cleaner Option)


Create a reusable config reader for all tests.


Config.java

public class Config {


    public static String get(String key) {

        // 1. Check Java system properties

        if (System.getProperty(key) != null) {

            return System.getProperty(key);

        }


        // 2. Check environment variables

        if (System.getenv(key) != null) {

            return System.getenv(key);

        }


        throw new RuntimeException("Missing config key: " + key);

    }

}


Usage:

driver.get(Config.get("BASE_URL"));


✔ Advantages:


One unified way to retrieve environment values


Works for local + CI + cloud execution


Eliminates repetitive getenv() and getProperty() calls


✅ 5. Real Selenium Example Combining It All

@Test

public void loginTest() {


    String url      = Config.get("BASE_URL");

    String username = Config.get("USERNAME");

    String password = Config.get("PASSWORD");


    WebDriver driver = new ChromeDriver();

    driver.get(url);


    driver.findElement(By.id("user")).sendKeys(username);

    driver.findElement(By.id("pass")).sendKeys(password);

    driver.findElement(By.id("loginBtn")).click();


    driver.quit();

}

๐ŸŽฏ When to Use What?

Method Best For Notes

System.getenv() Secure values (passwords, URLs) Stored at OS/CI level

System.getProperty() Switching environments via Maven Pass values dynamically

TestNG @Parameters XML-driven tests Works with CI, flexible

Utility class Large frameworks Clean, unified access

Learn Selenium with JAVA Training in Hyderabad

Read More

Using HashMap and ArrayList to Store Test Data

Parameterization in TestNG Using @DataProvider

Working with CSV Files in Java for Test Automation

Using JSON for Test Data in Java Selenium

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive