Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: global beforeEach/beforeAll hooks #32348

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/src/test-fixtures-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,63 @@ 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 declare them as auto fixtures like this:

```ts title="fixtures.ts"
import { test as base } from '@playwright/test';

export const test = base.extend({
forEachTest: [async ({ page, baseURL }, use) => {
// This code runs before every test.
await page.goto('http://localhost:8000');
yury-s marked this conversation as resolved.
Show resolved Hide resolved
await use();
// This code runs after every test.
console.log('Last URL:', page.url());
yury-s marked this conversation as resolved.
Show resolved Hide resolved
}, { auto: true }], // automatically starts for every test.
});
```

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 declare 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({
forEachWorker: [async ({}, use) => {
// This code runs before all the tests in the worker process.
console.log(`Starting test worker ${test.info().workerIndex}`);
await use();
// This code runs after all the tests in the worker process.
console.log(`Stopping test worker ${test.info().workerIndex}`);
}, { scope: 'worker', auto: true }], // automatically starts for every worker.
});
```

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.
Loading