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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What you can do is override the existing page fixture, then before the use() is your "beforeEach" and after the use() is the "afterEach": playwright.dev/docs/next/test-fixtures#overriding-fixtures for a beforeAll and afterAll as you said global-setup/teardown is the right place.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.