There are several ways to parametrize your tests by environment in Playwright.
One way is to use environment variables passed through the command line. Here's how you can set the username and password:
USERNAME=me PASSWORD=secret npx playwright test
To run tests against a staging or production environment, you can do:
STAGING=1 npx playwright test
You can also use .env
files to manage environment variables. For instance, you can use the dotenv
package to read environment variables directly in the configuration file.
Another way is by configuring projects for multiple environments. This allows you to run a group of tests with different timeouts or retries or against different environments.
For example, you can run your tests against a staging environment with 2 retries and a production environment with 0 retries. You can also split tests into projects and use filters to run a subset of tests.
Here's an example:
// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Staging',
use: {
baseURL: 'https://staging.ray.run',
retries: 2
}
},
{
name: 'Production',
use: {
baseURL: 'https://ray.run',
retries: 0
}
}
]
};
export default config;
In this setup, you can run only smoke-related test cases without any retries in one project, while another project runs all other test cases with some number of retries.
These are just a few ways to parametrize your Playwright tests for different environments. Choose the one that best fits your needs and preferences.
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].