Trying to move away from global setup to project dependencies --https://playwright.dev/docs/next/test-projects#dependencies.
I have a setup project which executes first. Then a project (e2e) for executing the tests. And then a cleanup project.
I am setting up an environment variable process.env.FOO for the first time in one of the tests in the setup project. This variable's value is accessible in the tests in e2e project and also in the cleanup project.
I have a custom reporter that implements playwright's reporter. But i could not access the environmental variable process.env.FOO in this custom reporter. It is undefined in onEnd , OnTestEnd for all the tests, in all the projects. Not sure if this is a problem with how playwright is handling the environment variables or problem with my code. Appreciate any help on this. Given the snippets below.
playwright.config.ts
require('dotenv').config();
export default defineConfig({
...
...
reporter: process.env.CI
? [
["./src/helpers/CustomReporter.ts", { outputFile: "results.xml" }],
]
: [
["html"],
],
projects: [
{
name: "Setup",
testMatch: "**/AdminLogin.setup.ts",
teardown: "Cleanup",
},
{
name: "e2e",
testIgnore: ["**/*.Login.setup.ts"],
dependencies: ["Setup"],
},
{
name: "Cleanup",
testMatch: "**/Termination.cleanup.ts",
},
})
AdminLogin.setup.ts
import { test as setup } from "@playwright/test";
setup("Login as admin", async ({ page}) => {
process.env.FOO = "21"
})
CustomReporter.ts
class JUnitReporter implements Reporter {
onBegin(config, suite) {
console.log('onBegin process.env.FOO = ' + process.env.FOO)
}
onTestEnd(test, result) {
console.log('onTestEnd process.env.FOO = ' + process.env.FOO)
}
onEnd(result) {
console.log('onEnd process.env.FOO = ' + process.env.FOO)
}
}
This thread is trying to answer question "Why is the environment variable set in a Playwright setup project not accessible in a custom reporter?"
Pls note that earlier I have setup process.env.FOO in global setup and it works fine and as expected in the same custom reporter. This problem comes only when i switched to project dependencies from using global setup. The only theory i have got is may be because the customreporter gets instantiated before any of the tests in the setup project is executed
For anyone looking solution for a similar problem ---> used the below approach
Setting attachments inside tests - you can read them later on in the reporter via https://playwright.dev/docs/api/class-testresult#test-result-attachments. This is commonly used to attach either files or just some text based data.
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].