How do I generate random environment variables in my Playwright test configuration using faker.js?


To generate random environment variables in your @playwright/test configuration using faker.js, follow these steps:

  1. Install faker.js by running npm install faker in your terminal.

  2. Import faker.js and other required modules in your configuration file:

    import { defineConfig } from '@playwright/test';
    import dotenv from 'dotenv';
    import path from 'path';
    import faker from 'faker';
  3. Load your .env file using dotenv.config():

    // Read from default ".env" file.
    // Alternatively, read from "../my.env" file.
    dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });
  4. Use faker.js methods to generate random values for your environment variables and pass them to your configuration object using the use property:

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

In this example, we generate a random email address for the EMAIL environment variable using faker.internet.email(). You can explore other faker.js methods to generate different types of data, such as names, addresses, or phone numbers. This approach adds randomness and variability to your tests without manually specifying each value.

Thank you!
Was this helpful?
Still have questions?

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

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.