To generate random environment variables in your @playwright/test configuration using faker.js, follow these steps:
Install faker.js by running npm install faker
in your terminal.
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';
Load your .env
file using dotenv.config()
:
// Read from default ".env" file.
dotenv.config();
// Alternatively, read from "../my.env" file.
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });
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.
If you still have questions, please ask a question and I will try to answer it.
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 [email protected].