From e81d982b381fc9a2356dec3a71fc8906b7fe1625 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 27 Aug 2024 16:03:46 -0700 Subject: [PATCH] docs: global beforeEach/beforeAll hooks Fixes https://github.com/microsoft/playwright/issues/9468 --- docs/src/test-fixtures-js.md | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/docs/src/test-fixtures-js.md b/docs/src/test-fixtures-js.md index a2e65e8849..cf75f34f28 100644 --- a/docs/src/test-fixtures-js.md +++ b/docs/src/test-fixtures-js.md @@ -722,3 +722,68 @@ export const test = base.extend({ }, { title: 'my fixture' }], }); ``` + +## Adding global beforeEach/afterEach hooks + +[`method: Test.beforeEach`] and [`method: Test.afterEach`] hooks run before/after each test declared in the same file and same [`method: Test.describe`] block (if any). If you want to declare hooks that run before/after each test globally, you can define them as auto fixtures with `scope: 'test'` like this: + +```ts title="fixtures.ts" +import { test as base } from '@playwright/test'; + +export const test = base.extend({ + sharedAfterEach: [ async ({ page, baseURL }, use) => { + await page.goto(baseURL); + await use(); + }, { scope: 'test', auto: true } ], // starts automatically before every test, we pass "auto" for that. + + sharedBeforeEach: [ async ({ page }, use) => { + await use(); + console.log('Test final URL:', page.url()); + }, { scope: 'test', auto: true } ], // starts automatically after every test, we pass "auto" for that. +}); +``` + +And then import the fixtures in all your tests: + +```ts title="mytest.spec.ts" +import { test } from './fixtures'; +import { expect } from '@playwright/test'; + +test('basic', async ({ page, baseURL }) => { + expect(page).toHaveURL(baseURL!); +}); +``` + +## Adding global beforeAll/afterAll hooks + +[`method: Test.beforeAll`] and [`method: Test.afterAll`] hooks run before/after all tests declared in the same file and same [`method: Test.describe`] block (if any) once per worker process. If you want to declare hooks +that run before/after all tests in every file, you can define them as auto fixtures with `scope: 'worker'` as follows: + +```ts title="fixtures.ts" +import { test as base } from '@playwright/test'; + +export const test = base.extend({ + sharedBeforeAll: [ async ({ browser }, use) => { + console.log('Before All', browser.version()); + await use(); + }, { scope: 'worker', auto: true } ], // starts automatically for every worker, we pass "auto" for that. + + sharedAfterAll: [ async ({ browser }, use) => { + console.log('After All', browser.version()); + await use(); + }, { scope: 'worker', auto: true } ], // starts automatically for every worker, we pass "auto" for that. +}); +``` + +And then import the fixtures in all your tests: + +```ts title="mytest.spec.ts" +import { test } from './fixtures'; +import { expect } from '@playwright/test'; + +test('basic', async ({ }) => { + // ... +}); +``` +Note that the fixtures will still run once per [worker process](./test-parallel.md#worker-processes) but you don't need to redeclare them in every file. +