How do I generate random data for environment variables in Playwright tests using Faker.js?


To generate random data for environment variables in your @playwright/test using Faker.js, follow these steps:

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

  2. Import the faker module in your configuration file and use it to generate random values for your environment variables. Here's an example:

import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';
import faker from 'faker';


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

In this example, we're using the faker.internet.email() method to generate a random email address for the EMAIL environment variable.

  1. Customize the generated data by passing options to the various methods provided by the faker module. For instance:
const firstName = faker.name.firstName();
const lastName = faker.name.lastName();
const fullName = `${firstName} ${lastName}`;

console.log(fullName); // e.g. John Doe

Here, we generate a random first name and last name using the name.firstName() and name.lastName() methods, and then combine them into a full name string.

By following these steps, you can easily generate randomized test data for your Playwright tests using Faker.js and environment variables.

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.