Environment Management

Introduction

As automation projects grow, the same test suite often needs to run against different environments such as Development (Dev), Quality Assurance (QA), Staging, and Production. Each environment has its own application URL, database, API endpoints, credentials, and other configuration values.

Hard-coding these values inside the automation scripts makes the framework difficult to maintain because every environment change requires modifications to the test code.

Environment Management solves this problem by allowing the framework to read environment-specific values from Environment Variables or configuration files. This enables the same automation code to execute against multiple environments without changing the test scripts.

In this tutorial, you’ll learn what Environment Management is, why it is important, how to manage different environments in Selenium frameworks, and how professional automation frameworks use environment variables.


What is Environment Management?

Environment Management is the process of configuring an automation framework to run the same test cases against different application environments without modifying the automation code.

Instead of hard-coding values such as application URLs and browser names, the framework reads them from environment variables or configuration files during execution.

For example:

                Automation Framework
                        │
                        ▼
              Environment Variables
                        │
        ┌───────────────┼───────────────┐
        ▼               ▼               ▼
      Local            QA          Production
        │               │               │
        ▼               ▼               ▼
 Different URLs and Configuration Values

This approach allows automation engineers to execute the same tests in multiple environments simply by changing the environment variables.


Why Use Environment Management?

Using Environment Management provides several benefits:

  • Eliminates hard-coded environment values.

  • Supports multiple execution environments.

  • Makes the framework flexible.

  • Reduces maintenance effort.

  • Simplifies environment switching.

  • Improves code reusability.

  • Makes Continuous Integration (CI/CD) pipelines easier to configure.


How to Implement Environment Management

Professional Selenium frameworks commonly use Environment Variables to store environment-specific values.

Some common environment variables include:

  • Application URL

  • Browser Name

  • Username

  • Password

  • API Endpoint

  • Database URL

  • Execution Environment


1. Define Environment Variables

Environment variables are created outside the automation code.

Example:

BASE_URL=https://qa.myapplication.com

BROWSER=chrome

These values can be changed without modifying the test scripts.


2. Read Environment Variables

Python provides the os.getenv() method to read environment variables.

Example:

import os

base_url = os.getenv("BASE_URL")
browser = os.getenv("BROWSER")

The framework automatically retrieves the required values at runtime.


3. Use the Environment Values

Instead of hard-coding values inside the tests, use the environment variables.

Example:

driver.get(base_url)

if browser == "chrome":
    driver = webdriver.Chrome()

Changing the environment variable automatically changes the behavior of the framework.


Common Environments Used in Automation

Professional automation frameworks usually execute against:

  • Local

  • Development (Dev)

  • QA (Testing)

  • Staging (Pre-Production)

  • Production

Each environment has its own configuration while the automation code remains exactly the same.


Example

import os

from selenium import webdriver


# Topic: 46. Framework Utilities - Environment Management
# Practice site: https://www.testmuai.com/selenium-playground/
# Run: pytest -s 46_examples/test_02_environment_management.py
#
# Environment variables switch between local, staging, and production without
# changing test code.


def test_environment_management(monkeypatch):
    monkeypatch.setenv(
        "BASE_URL",
        "https://www.testmuai.com/selenium-playground/",
    )
    monkeypatch.setenv("BROWSER", "chrome")

    base_url = os.getenv("BASE_URL")
    browser = os.getenv("BROWSER")

    driver = webdriver.Chrome() if browser == "chrome" else None
    assert driver is not None

    try:
        driver.get(base_url)
        assert "selenium-playground" in driver.current_url
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

import os
from selenium import webdriver

The os module is imported to read environment variables, while the webdriver module is imported to launch and control the browser.


Set Environment Variables

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

monkeypatch.setenv("BROWSER", "chrome")

The PyTest monkeypatch fixture is used to temporarily create environment variables during test execution.

In a real automation framework, these variables are usually defined in the operating system, CI/CD pipeline, or execution environment rather than inside the test.


Read Environment Variables

base_url = os.getenv("BASE_URL")

browser = os.getenv("BROWSER")

The os.getenv() function retrieves the values stored in the environment variables.

The framework now knows which application URL and browser should be used.


Create the Browser

driver = webdriver.Chrome() if browser == "chrome" else None

The browser is selected based on the value of the BROWSER environment variable.

Since the value is "chrome", a Chrome browser instance is created.

In large frameworks, multiple browsers such as Chrome, Edge, and Firefox are usually supported.


Verify Browser Creation

assert driver is not None

This assertion confirms that the browser object was successfully created before continuing the test.


Open the Application

driver.get(base_url)

The application URL is read from the environment variable instead of being hard-coded inside the test.

This makes it easy to switch between different environments.


Verify the Current URL

assert "selenium-playground" in driver.current_url

The test verifies that the browser successfully navigated to the expected website.


Close the Browser

driver.quit()

The browser session is closed after the test execution.


Practical Example

Suppose your company has three environments:

  • Development

  • QA

  • Production

Each environment has a different application URL.

Instead of changing the URL inside every Selenium test, the framework reads the BASE_URL environment variable. Changing this variable automatically allows the same test suite to execute against a different environment.


Automation Testing Example

Consider an online banking application.

Before every release, the automation suite must execute on the QA environment for testing and later on the Production environment for final validation.

The automation engineer simply changes the environment variables to point to the required environment, and the same Selenium test suite executes without any code modifications.


Real-World Example

Environment Management is commonly used in automation frameworks developed for:

  • Banking Applications

  • E-commerce Websites

  • Healthcare Systems

  • CRM Applications

  • ERP Systems

  • Insurance Portals

  • Government Applications

  • Enterprise Web Applications

In enterprise projects, CI/CD tools such as Jenkins, GitHub Actions, Azure DevOps, and GitLab CI/CD commonly set environment variables before executing Selenium automation suites.


Advantages of Environment Management

  • Eliminates hard-coded environment values.

  • Supports multiple execution environments.

  • Simplifies environment switching.

  • Improves framework flexibility.

  • Reduces maintenance effort.

  • Works well with CI/CD pipelines.

  • Keeps automation scripts clean.

  • Improves framework scalability.


Common Mistakes Beginners Make

Hard-Coding Environment URLs

Avoid writing application URLs directly inside the test scripts.

Always retrieve them from environment variables or configuration files.


Storing Sensitive Information in Code

Do not hard-code usernames, passwords, or API keys.

Store them securely using environment variables or secret management tools.


Creating Separate Test Scripts for Each Environment

The same automation code should execute against every environment.

Only the environment variables should change.


Forgetting to Validate Environment Variables

Always verify that required environment variables exist before using them.

Missing variables may cause test failures.


Best Practices

  • Store environment-specific values in environment variables.

  • Keep test scripts independent of environments.

  • Use meaningful environment variable names.

  • Validate environment variables before execution.

  • Avoid hard-coded URLs and credentials.

  • Use environment variables together with Configuration Management.

  • Integrate environment variables with CI/CD pipelines for automated execution.


Conclusion

Environment Management enables Selenium automation frameworks to execute the same test suite across multiple environments without modifying the test code. By storing environment-specific values in environment variables, the framework becomes more flexible, reusable, and easier to maintain. This approach is widely used in enterprise automation projects and plays a crucial role in Continuous Integration and Continuous Deployment (CI/CD) pipelines.


Frequently Asked Questions (FAQs)

What is Environment Management?

Environment Management is the process of executing the same automation framework against different environments using environment-specific configuration values.


Why is Environment Management important?

It allows the same test suite to run on different environments without modifying the automation code.


What are environment variables?

Environment variables are operating system or execution environment variables that store values such as URLs, browser names, credentials, and API endpoints.


Can Environment Management be used with Configuration Management?

Yes.

Most professional Selenium frameworks use both Configuration Management and Environment Management together to create flexible and scalable automation frameworks.


Is Environment Management used in enterprise automation frameworks?

Yes.

Almost every enterprise Selenium framework uses Environment Management to support Dev, QA, Staging, and Production environments while keeping the automation code unchanged.


Key Takeaways

  • Environment Management allows the same automation code to run in multiple environments.

  • Environment-specific values are stored in environment variables instead of hard-coded in test scripts.

  • It simplifies environment switching and reduces maintenance effort.

  • It integrates seamlessly with CI/CD pipelines and enterprise automation frameworks.

  • Environment Management is a standard practice in professional Selenium automation projects.