4

Referring the documentation provided by playwright, seems like the hooks (example: afterAll / beforeAll) can be used only inside a spec/ test file as below:

// example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
  console.log('Before tests');
});

test.afterAll(async () => {
  console.log('After tests');
});

test('my test', async ({ page }) => {
  // ...
});

My question: is there any support where there can be only one AfterAll() or beforeAll() hook in one single file which will be called for every test files ? the piece of code that i want to have inside the afterAll and beforeAll is common for all the test/ specs files and i dont want to have the same duplicated in all the spec files/ test file. Any suggestion or thoughts on this?

TIA Allen

Allen
  • 111
  • 1
  • 11

1 Answers1

4

Here is an update after my findings: Playwright does not support root level hooks. this is currently not possible as parallel tests will run in separate workers and each of them will run afterAll hook once the test completes. A preferred solution to the above concern will be using the global-setup and global-teardown.

Allen
  • 111
  • 1
  • 11
  • What you can do is override the existing page fixture, then before the use() is your "beforeEach" and after the use() is the "afterEach": https://playwright.dev/docs/next/test-fixtures#overriding-fixtures for a beforeAll and afterAll as you said global-setup/teardown is the right place. – Max Schmitt Mar 23 '22 at 10:52
  • 1
    @MaxSchmitt the problem I have with the page fixture is that I can not reuse the same page across multiple tests. I do not want the page to refresh like that. – dman Jul 19 '22 at 23:21