The test.beforeEach()
hook can be used inside a test.describe()
group. When it is called inside test.describe()
, it runs before each test in that group.
For example:
import { test, expect } from '@playwright/test';
test.describe('website test', () => {
test.beforeEach(async ({ page }, testInfo) => {
console.log(`Running ${testInfo.title}`);
await page.goto('https://www.designcise.com');
});
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
In this example, test.beforeEach()
will be called once for each of the two tests.
Please note that if multiple test.beforeEach()
hooks are added, they will run in the order of their registration.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.