Environment Variables

Introduction

Automation frameworks often need values that vary from one environment or machine to another, such as application URLs, browser names, usernames, passwords, API keys, database credentials, and access tokens. Storing these values directly inside automation scripts is not recommended because it makes the framework less secure and harder to maintain.

A better approach is to use Environment Variables. Environment variables are values stored outside the application code and are provided by the operating system. During execution, the automation framework reads these values whenever required.

Environment variables are especially useful for storing sensitive information and machine-specific settings because they keep confidential data out of the source code and version control systems.

In Selenium automation, environment variables are commonly used for managing different environments, storing credentials, switching browsers, and configuring application settings without modifying the automation scripts.

In this tutorial, you’ll learn what environment variables are, why they are used in Selenium automation, how to access them using Python, and how they improve the security and flexibility of automation frameworks.


What are Environment Variables?

Environment Variables are operating system variables that store configuration values outside the application code.

Programs can access these values during execution without hard-coding them into the source code.

Common environment variables used in Selenium automation include:

  • Application URL

  • Browser Name

  • Username

  • Password

  • API Keys

  • Database Credentials

  • Access Tokens

  • Execution Environment (QA, Staging, Production)

Example:

PLAYGROUND_URL=https://www.testmuai.com/selenium-playground/
BROWSER=chrome

The automation framework reads these values whenever the tests are executed.


Why Use Environment Variables?

Environment variables provide several advantages:

  • Keep sensitive information out of source code.

  • Improve application security.

  • Easily switch between environments.

  • Store machine-specific settings.

  • Avoid hard-coded values.

  • Simplify CI/CD pipeline configuration.

  • Improve framework maintainability.

  • Support reusable automation frameworks.


How to Set Environment Variables

Environment variables can be created in several ways depending on the operating system or execution environment.

Windows (Command Prompt)

set PLAYGROUND_URL=https://www.testmuai.com/selenium-playground/

Windows (PowerShell)

$env:PLAYGROUND_URL="https://www.testmuai.com/selenium-playground/"

Linux / macOS

export PLAYGROUND_URL=https://www.testmuai.com/selenium-playground/

In automation frameworks, environment variables are also commonly configured through Jenkins, GitHub Actions, Azure DevOps, Docker, or CI/CD pipelines.


Example

import os

from selenium import webdriver


def test_environment_variables(monkeypatch):
    monkeypatch.setenv(
        "PLAYGROUND_URL",
        "https://www.testmuai.com/selenium-playground/"
    )

    url = os.environ["PLAYGROUND_URL"]

    driver = webdriver.Chrome()

    try:
        driver.get(url)

        assert "selenium-playground" in driver.current_url

    finally:
        driver.quit()

Understanding the Code

Import Required Modules

import os

from selenium import webdriver

The required modules are imported.

  • os provides access to operating system functionality, including environment variables.

  • webdriver is used to launch and control the browser.


Set the Environment Variable

monkeypatch.setenv(
    "PLAYGROUND_URL",
    "https://www.testmuai.com/selenium-playground/"
)

The monkeypatch.setenv() method temporarily creates an environment variable named PLAYGROUND_URL.

This variable exists only during the test execution.

Note: This method is provided by PyTest and is mainly used for testing purposes. In real-world automation frameworks, environment variables are usually created outside the test script through the operating system, CI/CD pipelines, Docker containers, or cloud execution platforms.


Read the Environment Variable

url = os.environ["PLAYGROUND_URL"]

The value of the environment variable is retrieved using Python’s os.environ dictionary.

The variable url now contains:

https://www.testmuai.com/selenium-playground/

Launch the Browser

driver = webdriver.Chrome()

A new Chrome browser instance is created.


Open the Application

driver.get(url)

Instead of hard-coding the application URL, Selenium reads the URL from the environment variable and opens the application.


Verify the Current URL

assert "selenium-playground" in driver.current_url

The script verifies that the browser has successfully opened the expected application.

If the URL contains "selenium-playground", the test passes.


Close the Browser

finally:
    driver.quit()

The browser is closed after the test execution.


Practical Example

Suppose an e-commerce company has three environments:

  • Development

  • QA

  • Production

Instead of modifying Selenium scripts whenever the environment changes, the application URL is stored as an environment variable. Before execution, the automation framework reads the appropriate URL and automatically opens the correct environment.


Automation Testing Example

Consider an online banking application.

Sensitive information such as database credentials, API tokens, customer usernames, passwords, and application URLs are stored as environment variables. During execution, the automation framework reads these values securely without exposing confidential information inside the automation code.


Real-World Example

Environment variables are widely used in:

  • Selenium Automation Frameworks

  • Jenkins Pipelines

  • GitHub Actions

  • Azure DevOps

  • Docker Containers

  • Kubernetes Deployments

  • REST API Automation

  • Cloud Testing Platforms

  • Enterprise Automation Frameworks

Typical environment variables include application URLs, browser names, usernames, passwords, API keys, database credentials, access tokens, and execution environments.


Advantages of Using Environment Variables

  • Improve application security.

  • Prevent hard-coded credentials.

  • Simplify environment switching.

  • Support machine-specific configurations.

  • Reduce code modifications.

  • Work well with CI/CD pipelines.

  • Improve framework flexibility.

  • Protect sensitive information.


Common Mistakes Beginners Make

Hard-Coding Sensitive Information

Avoid storing usernames, passwords, API keys, or access tokens directly inside Selenium scripts.


Assuming Environment Variables Always Exist

Always verify that required environment variables are available before using them.


Confusing Configuration Files with Environment Variables

Configuration files store general framework settings, while environment variables are commonly used for sensitive or machine-specific values.


Committing Secrets to Version Control

Never store confidential information such as passwords or API keys in Git repositories.


Best Practices

  • Store sensitive information in environment variables.

  • Keep secrets out of source code.

  • Use meaningful environment variable names.

  • Validate environment variables before use.

  • Use separate environment variables for different environments.

  • Combine environment variables with configuration files for better flexibility.

  • Use CI/CD tools to manage environment variables securely.


Conclusion

Environment Variables provide a secure and flexible way to store machine-specific settings and sensitive information outside the automation code. By reading these values during execution, Selenium frameworks become easier to maintain, more secure, and capable of running across multiple environments without changing the test scripts. They are a standard practice in professional automation frameworks and CI/CD pipelines.


Frequently Asked Questions (FAQs)

What are environment variables?

Environment variables are operating system variables used to store configuration values outside the application code.


Why are environment variables used in Selenium automation?

They help store sensitive information such as URLs, credentials, API keys, and browser settings without hard-coding them into test scripts.


Which Python module is used to access environment variables?

Python provides the built-in os module to access environment variables.


What is os.environ?

os.environ is a dictionary-like object that stores all available environment variables and their values.


Are environment variables used in professional automation frameworks?

Yes.

Environment variables are widely used in Selenium automation frameworks, DevOps workflows, CI/CD pipelines, cloud testing platforms, and enterprise applications to securely manage configuration and sensitive information.


Key Takeaways

  • Environment Variables store configuration values outside the application code.

  • Python uses the built-in os module to access environment variables.

  • They improve security by keeping sensitive information out of source code.

  • Environment variables simplify switching between different execution environments.

  • They are widely used in Selenium frameworks, CI/CD pipelines, Docker, Kubernetes, and enterprise automation projects.