DEV Community

Cover image for ๐ŸŒ Building Playwright Framework Step By Step - Setup Environment Variables
idavidov13
idavidov13

Posted on โ€ข Edited on

3 1 1 1 1

๐ŸŒ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ .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
Enter fullscreen mode Exit fullscreen mode

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

URL=
API_URL=
USER_NAME=
EMAIL=
PASSWORD=
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›ก๏ธ 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';
Enter fullscreen mode Exit fullscreen mode

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,
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก 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'
Enter fullscreen mode Exit fullscreen mode

macOS/Linux:

export ENVIRONMENT=staging
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify Environment Setting

Check which environment is active:

Windows:

echo $env:ENVIRONMENT
Enter fullscreen mode Exit fullscreen mode

macOS/Linux:

echo $ENVIRONMENT
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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!

AWS Security LIVE! Stream

Streaming live from AWS re:Inforce

Join AWS Security LIVE! at re:Inforce for real conversations with AWS Partners.

Learn More

Top comments (2)

Collapse
 
queen911 profile image
Qareena โ€ข

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

Collapse
 
idavidov13 profile image
idavidov13 โ€ข

Thank you!
I checked the guide and can say that you have done a great job with it. ๐Ÿ™Œ

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

๐Ÿ‘‹ Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someoneโ€™s dayโ€”leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay