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 2 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
65 changes: 65 additions & 0 deletions docs/src/test-fixtures-js.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check warning on line 1 in docs/src/test-fixtures-js.md

View workflow job for this annotation

GitHub Actions / Lint snippets

ts linting error

Error: This line has a length of 106. Maximum allowed is 100. 5 | await page.goto(baseURL); 6 | await use(); > 7 | }, { scope: 'test', auto: true } ], // starts automatically before every test, we pass "auto" for that. | ^ 8 | 9 | sharedBeforeEach: [async ({ page }, use) => { 10 | await use(); Unable to lint: 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. });

Check warning on line 1 in docs/src/test-fixtures-js.md

View workflow job for this annotation

GitHub Actions / Lint snippets

ts linting error

Error: This line has a length of 107. Maximum allowed is 100. 5 | console.log('Before All', browser.version()); 6 | await use(); > 7 | }, { scope: 'worker', auto: true } ], // starts automatically for every worker, we pass "auto" for that. | ^ 8 | 9 | sharedAfterAll: [async ({ browser }, use) => { 10 | console.log('After All', browser.version()); Unable to lint: 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. });
id: test-fixtures
title: "Fixtures"
---
Expand Down Expand Up @@ -722,3 +722,68 @@
}, { 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) => {
yury-s marked this conversation as resolved.
Show resolved Hide resolved
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());
yury-s marked this conversation as resolved.
Show resolved Hide resolved
}, { 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());
yury-s marked this conversation as resolved.
Show resolved Hide resolved
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.

Loading