Skip to content
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

Closed
AlexKomanov opened this issue Dec 29, 2022 · 15 comments
Closed

[Feature] Global config of beforeEach and afterEach #19766

AlexKomanov opened this issue Dec 29, 2022 · 15 comments

Comments

@AlexKomanov
Copy link
Contributor

AlexKomanov commented Dec 29, 2022

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!

@aslushnikov
Copy link
Collaborator

@AlexKomanov so far we'd recommend to use auto fixtures for this. Would it work for you?

@AlexKomanov
Copy link
Contributor Author

@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...)

@debs-obrien
Copy link
Collaborator

here is the example of the docs for auto fixtures incase it helps @AlexKomanov
https://playwright.dev/docs/test-fixtures#automatic-fixtures

@AlexKomanov
Copy link
Contributor Author

@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

@mxschmitt
Copy link
Member

@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.

@AlexKomanov
Copy link
Contributor Author

@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';

@mxschmitt
Copy link
Member

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.

@AlexKomanov
Copy link
Contributor Author

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.

@PeterHewat
Copy link

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!

@4ekki
Copy link

4ekki commented May 22, 2023

Probably related to #9468

@JanMosigItemis
Copy link

Up

@nitzanashi
Copy link

Any Update on this?

@LuisFloresSerna
Copy link

Up

@mxschmitt
Copy link
Member

Folding into #9468

@k4mr4n
Copy link

k4mr4n commented Jun 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants