๐ 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
๐ฆ 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!
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.