I want to keep project definitions in the playwright config file like:
projects: [
{
name: 'chrome',
use: { browserName: 'chromium' },
},
{
name: 'firefox',
use: { browserName: 'firefox' },
}
....
]
But instead of selecting the project from command line with
--project=chrome
I want to do that in my .env file. Is it possible?
Tried overriding the process.env.project value but does not work. I´m already reading values from .env file for other variables on my tests...
Yes it possible using the filter()
method on projects
array.
Here is a code example:
// playwright.config.ts
import { defineConfig, devices } from "@playwright/test";
require("dotenv").config();
const selectedProject = process.env.PROJECT || "all";
export default defineConfig({
testDir: "./tests",
use: { headless: false },
projects: [
{
name: "chrome",
use: { ...devices["Desktop Chrome"] },
},
{
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},
].filter(
(project) => project.name === selectedProject || selectedProject === "all"
), // Optionally use "all" to run all projects,
});
Make sure to set the PROJECT
env variable in your dotenv file,
with one of the defined project names, for example:
PROJECT = "firefox"
In this answer I've added the default option all
in order to avoid the no tests found
error in case of not setting the PROJECT
env variable.
Please take a note though, you can remove the default option if you like, or even enhance this logic to be able to handle multi-project selection.