playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Global config of beforeEach and afterEach

Open AlexKomanov opened this issue 2 years ago • 10 comments

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 avatar Dec 29 '22 06:12 AlexKomanov

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

aslushnikov avatar Dec 30 '22 06:12 aslushnikov

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

AlexKomanov avatar Dec 30 '22 07:12 AlexKomanov

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

debs-obrien avatar Jan 03 '23 20:01 debs-obrien

@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 avatar Jan 04 '23 10:01 AlexKomanov

@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 avatar Jan 04 '23 16:01 mxschmitt

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

AlexKomanov avatar Jan 05 '23 13:01 AlexKomanov

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 avatar Jan 05 '23 20:01 mxschmitt

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.

AlexKomanov avatar Jan 05 '23 21:01 AlexKomanov

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!

PeterHewat avatar Apr 06 '23 10:04 PeterHewat

Probably related to https://github.com/microsoft/playwright/issues/9468

4ekki avatar May 22 '23 13:05 4ekki

Up

JanMosigItemis avatar Sep 25 '23 09:09 JanMosigItemis

Any Update on this?

nitzanashi avatar Jun 10 '24 14:06 nitzanashi

Up

LuisFloresSerna avatar Jun 14 '24 17:06 LuisFloresSerna

Folding into https://github.com/microsoft/playwright/issues/9468

mxschmitt avatar Jun 14 '24 20:06 mxschmitt

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

k4mr4n avatar Jun 28 '24 14:06 k4mr4n