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.

    Details

    When called in the scope of a test file, runs before each test in the file. When called inside a test.describe(title, callback) group, runs before each test in the group. If multiple beforeEach hooks are added, they will run in the order of their registration.

    You can access all the same Fixtures as the test function 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(hookFunction) to teardown any resources set up in beforeEach.

    Usage

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

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

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

    Parameters

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

    Returns void