Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root level Hooks in playwright test

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

like image 314
Allen Avatar asked Oct 27 '25 06:10

Allen


1 Answers

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.

like image 134
Allen Avatar answered Oct 29 '25 23:10

Allen