-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Global config of beforeEach and afterEach #19766
Comments
@AlexKomanov so far we'd recommend to use auto fixtures for this. Would it work for you? |
@aslushnikov 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. |
here is the example of the docs for auto fixtures incase it helps @AlexKomanov |
@debs-obrien 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 |
@mxschmitt 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. |
@mxschmitt 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: ...
// 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 |
Probably related to #9468 |
Up |
Any Update on this? |
Up |
Folding into #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 |
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!
The text was updated successfully, but these errors were encountered: