Sure, you can run tests and inject environment variables in Playwright. Use the npx playwright test
command to run tests. To inject environment variables, set them in the command line like this: USERNAME=me PASSWORD=secret npx playwright test
.
You can also use a configuration file, playwright.config.ts
, to define your base URL and other configurations using environment variables. Here's an example:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
Run your tests with different values for the STAGING
environment variable to switch between environments.
If you prefer, use .env
files to manage environment variables. Create a .env
file with key-value pairs:
STAGING=0
USERNAME=me
PASSWORD=secret
Then, in your playwright.config.ts
, read these values using a package like dotenv:
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/',
},
});
This way, you can edit the .env
file to set any variables you'd like without modifying your test code. Playwright provides multiple ways to run tests and inject environment variables. You can customize your test runs based on different environments or configurations.
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].