microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.41k stars 3.63k forks source link

[Question] Not able to access environmental variables created during a Setup project in custom reporter #29949

Closed bc-rajesh closed 7 months ago

bc-rajesh commented 7 months ago

Trying to move away from global setup to project 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 use a custom reporter and 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 even though it is perfectly accessible in the e2e and cleanup projects. Can anyone pls provide some info of whether is this a bug or is this how the environment variables are supposed to behave?

Pls note that this variable was accessible when it was created in global setup. It is not accessible only when it is created in the setup project.

code snippet 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)
  }
}
mxschmitt commented 7 months ago

We recommend 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. Would that work for you?

bc-rajesh commented 7 months ago

Thank you. This works for me 🙇‍♂️