๐ŸŒ Building Playwright Framework Step By Step - Setup Environment Variables

Ivan DavidovIvan Davidov
3 min read

๐ŸŽฏ Introduction

Environment variables are the backbone of professional test automation - they're your secret weapon for creating flexible, secure, and maintainable test frameworks! ๐Ÿ”

๐Ÿ’ก What are Environment Variables?: Configuration values stored outside your code that control how your application behaves in different environments

๐Ÿš€ Why Environment Variables Matter

Environment variables provide several critical benefits:

  • ๐Ÿ”’ Security - Keep sensitive data out of your codebase

  • ๐ŸŽฏ Flexibility - Switch between environments seamlessly

  • ๐Ÿ”ง Maintainability - Centralized configuration management

  • ๐Ÿ‘ฅ Team Collaboration - Consistent setup across team members

๐Ÿ“ฆ Installing dotenv

First, let's install the dotenv package to manage our environment variables:

npm install dotenv
npm install --save-dev @types/dotenv

๐Ÿ’ก Pro Tip: The @types/dotenv package provides TypeScript support for better development experience

๐Ÿ“ Create Environment Files Structure

Step 1: Create Environment Folder

In your project's root directory, create an env folder and separate .env.environmentName files:

project-root/
โ”œโ”€โ”€ env/
โ”‚   โ”œโ”€โ”€ .env.dev
โ”‚   โ”œโ”€โ”€ .env.staging  
โ”‚   โ”œโ”€โ”€ .env.prod
โ”‚   โ””โ”€โ”€ .env.example
โ”œโ”€โ”€ tests/
โ””โ”€โ”€ playwright.config.ts

Step 2: Define Environment Variables

Create your environment-specific files with the following structure:

๐Ÿ“„ .env.dev

URL=https://conduit.bondaracademy.com/
API_URL=https://conduit-api.bondaracademy.com/
USER_NAME=yourDevName
EMAIL=dev@example.com
PASSWORD=yourDevPassword

๐Ÿ“„ .env.staging

URL=https://staging.conduit.bondaracademy.com/
API_URL=https://staging-api.conduit.bondaracademy.com/
USER_NAME=yourStagingName
EMAIL=staging@example.com
PASSWORD=yourStagingPassword

๐Ÿ“„ .env.example (Template file)

URL=
API_URL=
USER_NAME=
EMAIL=
PASSWORD=

โš ๏ธ Important: Keep different values for every environment to ensure proper isolation

๐Ÿ”’ Exclude Environment Files from Version Control

Step 1: Update .gitignore

Add the following lines to your .gitignore file:

# Environment files
.env.*
!.env.example

๐Ÿ›ก๏ธ Security Best Practice: Never commit actual environment files to version control. Only commit the .env.example template

โš™๏ธ Configure Environment Utilization

Step 1: Import dotenv in playwright.config.ts

Add the import at the top of your playwright.config.ts file:

import dotenv from 'dotenv';

Step 2: Configure Environment Loading

Add this configuration logic to your playwright.config.ts:

// Determine which environment file to load
const environmentPath = process.env.ENVIRONMENT 
    ? `./env/.env.${process.env.ENVIRONMENT}`
    : `./env/.env.dev`; // Default to dev environment

// Load the environment variables
dotenv.config({
    path: environmentPath,
});

๐Ÿ’ก How it works:

  • If ENVIRONMENT is set, it loads .env.{ENVIRONMENT}

  • If not set, it defaults to .env.dev

๐ŸŽฎ Using Environment Variables

Step 1: Set Environment (Optional)

To use a specific environment, set the environment variable before running tests:

Windows (PowerShell):

$env:ENVIRONMENT='staging'

macOS/Linux:

export ENVIRONMENT=staging

Step 2: Verify Environment Setting

Check which environment is active:

Windows:

echo $env:ENVIRONMENT

macOS/Linux:

echo $ENVIRONMENT

Step 3: Access Variables in Tests

Use environment variables in your test files:

// Basic usage
const url = process.env.URL;
const apiUrl = process.env.API_URL;

// With type safety and defaults
const baseUrl = process.env.URL || 'http://localhost:3000';
const username = process.env.USER_NAME || 'defaultUser';

๐ŸŽฏ What's Next?

In the next article we will dive into Design Patterns - choosing between POM and Functional Helpers!

๐Ÿ’ฌ Community: Please feel free to initiate discussions on this topic, as every contribution has the potential to drive further refinement.


โœจ Ready to supercharge your testing skills? Let's continue this journey together!

0
Subscribe to my newsletter

Read articles from Ivan Davidov directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ivan Davidov
Ivan Davidov

Automation QA Engineer, ISTQB CTFL, PSM I, helping teams improve the quality of the product they deliver to their customers. โ€ข Led the development of end-to-end (E2E) and API testing frameworks from scratch using Playwright and TypeScript, ensuring robust and scalable test automation solutions. โ€ข Integrated automated tests into CI/CD pipelines to enhance continuous integration and delivery. โ€ข Created comprehensive test strategies and plans to improve test coverage and effectiveness. โ€ข Designed performance testing frameworks using k6 to optimize system scalability and reliability. โ€ข Provided accurate project estimations for QA activities, aiding effective project planning. โ€ข Worked with development and product teams to align testing efforts with business and technical requirements. โ€ข Improved QA processes, tools, and methodologies for increased testing efficiency. โ€ข Domain experience: banking, pharmaceutical and civil engineering. Bringing over 3 year of experience in Software Engineering, 7 years of experience in Civil engineering project management, team leadership and project design, to the table, I champion a disciplined, results-driven approach, boasting a record of more than 35 successful projects.