How do I set up environment variables in a Playwright Test configuration file?


Sure, you can set environment variables in your Playwright Test configuration file. By default, Playwright Test uses process.env for this. If you're specifying a port or URL, Playwright Test will wait for it to be available before running tests.

For continuous integration, you might want to use the reuseExistingServer option. This option doesn't use an existing server on the CI.

To see the stdout, you can set the DEBUG=pw:webserver environment variable.

Here's how you can set the testOptions.baseURL in your configuration file:

import { defineConfig } from '@playwright/test';

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

This allows your tests to use relative URLs.

If you want to manage your environment variables more easily, consider using .env files and the dotenv package. This lets you read environment variables directly from .env files and edit them without modifying your code.

If these options don't meet your needs, you might want to create tests via a CSV file instead of defining environment variables in config files.


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 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.