You can manage a large number of environment variables in @playwright/test in several ways.
One way is to use command line arguments when running tests. For instance, you can pass the secret username and password like this:
USERNAME=me PASSWORD=secret npx playwright test
Another way is to use a configuration file, such as playwright.config.ts
. Here, you can define different configurations for each environment.
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
To make managing environment variables easier, consider using .env
files. You can use a package like dotenv
to read these variables directly in your configuration file.
import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config();
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
Lastly, you can use a CSV file to store and manage your environment variables. Since Playwright runs in Node.js, you can directly read files from the file system and parse them with your preferred CSV library.
These approaches provide effective ways to handle a large number of environment variables across multiple environments while ensuring sensitive information is securely stored.
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].