๐ Building Playwright Framework Step By Step - Setup Environment Variables

๐ฏ 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
โ Prerequisites
This article builds directly on the concepts from previous ones. To get the most out of it, you should have:
๐ฆ 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/dotenvpackage 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. Only dev URL is working, files for other environments are only for demonstration purposes.
๐ 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.exampletemplate
โ๏ธ 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
ENVIRONMENTis 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';
You can learn more about Basic Types and Custom Types it TypeScript.
๐ฏ 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!
๐๐ป Thank you for reading! Building robust, scalable automation frameworks is a journey best taken together. If you found this article helpful, consider joining a growing community of QA professionals ๐ who are passionate about mastering modern testing.
Join the community and get the latest articles and tips by signing up for the newsletter.
About the author
- โพ๏ธ Agentic Quality Engineer ยท Creator of the Agentic Playwright Framework ยท Founder of ArchQA ยท Global Agentic Playwright Test Automation Workshops ยท Trained 4k+ engineers
Comments
Join the discussionYou are welcome, Misha! My goal was to explain in details one method, which I found most suitable for huge real world app with test integration in CI/CD. Keep in mind that Artem shared a few different ways to do it and has the boundary to do it in a set timeframe. The goal of his course is to show everything from getting the locators all the wau to custom fixtures. It is not a straight forward task, and in my opinion he has done a fantastic job. For example, I begin with his course too, and I am very thankful for great fundamentals he tought me.
Ivan Davidov 100%- Artem did a great job! But for me the env variables topic was explained very unclearly. And I'm glad that I found your article!
Ivan Davidov BTW, Exploring this topic I found a slightly different approach for setting playwright config:
import as dotenv from 'dotenv' import as path from 'path'
const ENV = process.env.ENVIRONMENT || 'dev'
const envPath = path.resolve(__dirname, ./env/.env.${ENV})
dotenv.config({ path: envPath })
Misha, it is great way to make the code easier to understand. You have done a great job! Be curious and keep experimenting ๐
Misha, yes, I agree with you! The main goal of my blog was to document every major topic, relevent to my work, so I have one place where I can find everything I need. In my articles you will find great insights from many great guys - Stefan Judis, Murat Ozcan, Artem Bondar and Filip Hric. If you are curious, you can check their fantastic work.
Ivan Davidov Oh, great, I'll check them, and thank you for your work!
Misha you are more than welcome!
Building Playwright Framework Step By Step
Master Playwright automation! This step-by-step series explains 'how' & 'why' to build robust, scalable test frameworks. A practical, educational articles providing a solid foundation for all skill levels.






