What is the process to update and reread environment variables from a .env file in Playwright?


Updating Environment Variables in Playwright

First, create a .env file in your project directory and set your email variable, like so: EMAIL=myemail@example.com.

Next, in your playwright.config.ts file, import and configure the dotenv package to read the .env file:

import dotenv from 'dotenv';
import path from 'path';

dotenv.config({ path: path.resolve(__dirname, '..', '.env') });

In your configuration options, you can now access your environment variables using process.env:

export default defineConfig({
  use: {
      process.env.STAGING === '1'
        ? 'http://staging.ray.run/'
        : 'http://ray.run/',
    email: process.env.EMAIL,

When you run your tests with npx playwright test, Playwright will use the updated values from the .env file.

Alternatively, you can set environment variables directly in your command line:

EMAIL=myemail@example.com npx playwright test

This will override the .env file. Playwright provides multiple ways to set and update environment variables, so choose the method that suits your needs best.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.