Skip to main content

beforeEach

Callable

  • beforeEach(inner: (args: PlaywrightTestArgs & PlaywrightTestOptions & Omit<SerenityOptions, actors> & SerenityFixtures & PlaywrightWorkerArgs & PlaywrightWorkerOptions & object, testInfo: TestInfo) => any): void
  • beforeEach(title: string, inner: (args: PlaywrightTestArgs & PlaywrightTestOptions & Omit<SerenityOptions, actors> & SerenityFixtures & PlaywrightWorkerArgs & PlaywrightWorkerOptions & object, testInfo: TestInfo) => any): void

  • Declares a beforeEach hook that is executed before each test.

    When called in the scope of a test file, runs before each test in the file. When called inside a test.describe([title, details, callback]) group, runs before each test in the group.

    You can access all the same Fixtures as the test body itself, and also the TestInfo object that gives a lot of useful information. For example, you can navigate the page before starting the test.

    You can use test.afterEach([title, hookFunction]) to teardown any resources set up in beforeEach.

    • test.beforeEach(hookFunction)
    • test.beforeEach(title, hookFunction)

    Details

    When multiple beforeEach hooks are added, they will run in the order of their registration.

    Playwright will continue running all applicable hooks even if some of them have failed.

    Usage

    // example.spec.ts
    import { test, expect } from '@playwright/test';

    test.beforeEach(async ({ page }) => {
    console.log(`Running ${test.info().title}`);
    await page.goto('https://my.start.url/');
    });

    test('my test', async ({ page }) => {
    expect(page.url()).toBe('https://my.start.url/');
    });

    Alternatively, you can declare a hook with a title.

    // example.spec.ts
    test.beforeEach('Open start URL', async ({ page }) => {
    console.log(`Running ${test.info().title}`);
    await page.goto('https://my.start.url/');
    });

    Parameters

    • inner: (args: PlaywrightTestArgs & PlaywrightTestOptions & Omit<SerenityOptions, actors> & SerenityFixtures & PlaywrightWorkerArgs & PlaywrightWorkerOptions & object, testInfo: TestInfo) => any

    Returns void