To generate random data for environment variables in your @playwright/test using Faker.js, follow these steps:
Install the faker.js package by running npm install faker
in your terminal.
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';
dotenv.config();
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.
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.
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].