To handle sensitive environment variables in Playwright, you can use the following methods:
USERNAME=me PASSWORD=secret npx playwright test
defineConfig
from '@playwright/test' and read environment variables.import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
Run tests against a staging or production environment:
STAGING=1 npx playwright test
dotenv
package to read environment variables directly in the configuration file.import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';
// Read from default ".env" file.
dotenv.config();
// Alternatively, read from "../my.env" file.
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
Edit the .env
file to set variable values.
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].