More comments

This commit is contained in:
Yury Semikhatsky 2024-08-27 16:42:15 -07:00
parent c8b52b37ae
commit 9461a21c8f

View file

@ -725,17 +725,19 @@ export const test = base.extend({
## Adding global beforeEach/afterEach hooks ## 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 with `scope: 'test'` like this: [`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" ```ts title="fixtures.ts"
import { test as base } from '@playwright/test'; import { test as base } from '@playwright/test';
export const test = base.extend({ export const test = base.extend({
forEachTest: [async ({ page, baseURL }, use) => { forEachTest: [async ({ page, baseURL }, use) => {
// This code runs before every test.
await page.goto('http://localhost:8000'); await page.goto('http://localhost:8000');
await use(); await use();
// This code runs after every test.
console.log('Last URL:', page.url()); console.log('Last URL:', page.url());
}, { scope: 'test', auto: true } ], // starts automatically for every test, we pass "auto" for that. }, { auto: true }], // starts automatically for every test, we pass "auto" for that.
}); });
``` ```
@ -759,11 +761,13 @@ that run before/after all tests in every file, you can declare them as auto fixt
import { test as base } from '@playwright/test'; import { test as base } from '@playwright/test';
export const test = base.extend({ export const test = base.extend({
forEachWorker: [async ({ browser }, use) => { forEachWorker: [async ({}, use) => {
console.log('Before All', browser.version()); // This code runs before all the tests in the worker process.
console.log(`Starting test worker ${test.info().workerIndex}`);
await use(); await use();
console.log('After All', browser.version()); // This code runs after all the tests in the worker process.
}, { scope: 'worker', auto: true } ], // starts automatically for every worker, we pass "auto" for that. console.log(`Stopping test worker ${test.info().workerIndex}`);
}, { scope: 'worker', auto: true }], // starts automatically for every worker, we pass "auto" for that.
}); });
``` ```