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.76k stars 3.66k forks source link

[Feature]: Passing variables from playwright.config to test.spec #32489

Closed hsiaalex closed 1 month ago

hsiaalex commented 2 months ago

🚀 Feature Request

Based on https://github.com/microsoft/playwright/issues/19010#issuecomment-1325555092 I am adjusting the HTML reporter output path so I can keep copies of previous test runs.

My test.spec takes some screenshots, I would like to use the same outputFolder to keep everything together.

Example

playwright-report/ ├── outputFolder/ │ ├── index.html │ ├── screenshot.png │ └── trace.zip └── another outputFolder/

Motivation

Define some variables in playwright.config that can be used anywhere else. In this example, I want to organize everything from the run into the same outputFolder.

pavelfeldman commented 2 months ago

My test.spec takes some screenshots, I would like to use the same outputFolder to keep everything together.

I'm not sure I understand this. HTML reporter will always copy all the resources to under its folder for sharing. Your feature title does not seem to be related to the feature description though.

hsiaalex commented 1 month ago

I have this code in my playwright.config.ts

const currentDateTime = new Date();
const adjustedDateTime = new Date(currentDateTime - currentDateTime.getTimezoneOffset() * 60 * 1000).toISOString().replace(/[:.]/g, "_").slice(0, -1);
const outputFolder = `./playwright-report/${adjustedDateTime}`;

export default defineConfig({
  reporter: [["html", { outputFolder }]],
  // project configs
});

In my test.spec.ts I have

await page.screenshot({ path: `${testInfo.project.name}.png` });

I want to use outputFolder from playwright.config.ts so my test.spec.ts could be:

await page.screenshot({ path: `${outputFolder}/${testInfo.project.name}.png` });
dgozman commented 1 month ago

I want to use outputFolder from playwright.config.ts so my test.spec.ts could be:

await page.screenshot({ path: `${outputFolder}/${testInfo.project.name}.png` });

I wouldn't recommend that. You are putting files inside the html report's directory. Html reporter assumes ownership of its output directory, so it could easily remove any files there. I believe it already does that today.


On your question about passing values from config to test file, you can export/import as with any javascript file:

// playwright.config.ts

export const someValue = 'value';
// example.spec.ts

import { someValue } from '../playwright.config';

Let me know whether that helps.

hsiaalex commented 1 month ago

On your question about passing values from config to test file, you can export/import as with any javascript file:

// playwright.config.ts

export const someValue = 'value';
// example.spec.ts

import { someValue } from '../playwright.config';

Let me know whether that helps.

I tried this shortly after posting my question, it worked (test.spec.ts could import outputFolder) but HTML reporter wasn't outputting. I'll have to take another look, but I might just use a different directory so my screenshots don't get removed accidentally (by HTML reporter) - or I might use test.attach to include (copies of) the screenshots in the HTML report.

dgozman commented 1 month ago

Closing as per above. To recap: