๐ฏ 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! 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.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!
Top comments (2)
This walkthrough is super helpful! ๐ Setting up environment variables is a key first step for anyone building a Playwright testing framework. If anyoneโs looking to go deeper into testing concepts, we just published a guide over at Agami Technologies that might be useful too. ๐๐ก
Playwright #Testing #Automation #DevTools
Thank you!
I checked the guide and can say that you have done a great job with it. ๐