playwright
playwright copied to clipboard
[Feature] Global config of beforeEach and afterEach
Hello Playwright team!
Maybe I’m missing that, but didn’t find this feature.
It will be great to have option for global configuration in playwright.config
of beforeEach and afterEach hooks. For example, to collect system logs and save them for every single test.
In case I’ve missed this functionality, I will be happy to see your solution.
Thanks a lot!
@AlexKomanov so far we'd recommend to use auto fixtures for this. Would it work for you?
@aslushnikov Can you give an example of this?
In my case - I need to add some functionality for 1500 tests in ~800 spec files. I need to add several commands that will take place before each and after each test.
I guess that I can handle the situation by extending the test object, but it will require adding new functionality in each test file. In the case of the global config of test.beforeEach
and test.afterEach
hooks - it can work like the other global functions (timeout, screenshots, etc...)
here is the example of the docs for auto fixtures incase it helps @AlexKomanov https://playwright.dev/docs/test-fixtures#automatic-fixtures
@debs-obrien It can help, but it will require a change in every existing test.
In the case of adding a global config - this case can be solved in an easier way.
So I still think that adding a global config can be really useful.
cc: @aslushnikov
@AlexKomanov see here for auto fixtures: https://playwright.dev/docs/test-fixtures#automatic-fixtures
If you have a auto worker fixture it will run before and/or after for each worker If you have a auto test fixture, it will run before and/or after each test.
@mxschmitt Thanks for the response. I guess it is the part that you noticed.
Can you please provide a practical example of the usage of this block? It's not so clear (at least for me). As I understand, in order to use the extended parts - you should call them from the tests.
// my-test.ts
import * as debug from 'debug';
import * as fs from 'fs';
import { test as base } from '@playwright/test';
export const test = base.extend<{ saveLogs: void }>({
saveLogs: [async ({}, use, testInfo) => {
// Collecting logs during the test.
const logs = [];
debug.log = (...args) => logs.push(args.map(String).join(''));
debug.enable('myserver');
await use();
// After the test we can check whether the test passed or failed.
if (testInfo.status !== testInfo.expectedStatus) {
// outputPath() API guarantees a unique file name.
const logFile = testInfo.outputPath('logs.txt');
await fs.promises.writeFile(logFile, logs.join('\n'), 'utf8');
testInfo.attachments.push({ name: 'logs', contentType: 'text/plain', path: logFile });
}
}, { auto: true }],
});
export { expect } from '@playwright/test';
See here how to use the fixtures: https://playwright.dev/docs/test-fixtures#using-a-fixture
TL;DR: You define them in one file and import them in the test files and use the new fixture instance instead of test to declare your tests.
TL;DR: You define them in one file and import them in the test files and use the new fixture instance instead of test to declare your tests.
@mxschmitt As you mentioned - usage of the auto-fixture will require opening each test file (in our case it is about 1000 spec .ts files) and importing the fixture in order to use some functionality.
In my opinion, adding the option to configure the before and after each inside the playwright.config file - will add amazing possibilities and not just for my case.
Hi all, I have the same issue and have done what @mxschmitt suggested: I have a base.fixture.ts with:
...
// Prevent snapshots from using browser and platform name in saved file path
test.beforeEach(async ({}, testInfo) => {
testInfo.snapshotPath = (name: string) => `${testInfo.file}-snapshots/${name}`;
});
export default test;
And it works in all tests that use this base fixture except one that also has a test.beforeEach()...
I guess that the beforeEach in the base fixture is being squashed by the beforeEach in my test. Is this so ?
In any cas, a global beforeEach (that doesn't get squashed) would be greate!
Probably related to https://github.com/microsoft/playwright/issues/9468
Up
Any Update on this?
Up
Folding into https://github.com/microsoft/playwright/issues/9468
It seems like this feature has been requested since 2021 and is still not updated. Using fixtures is not a well-engineered solution at all. I hope we get this feature soon