Skip to main content

afterEach

Callable

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

  • Declares an afterEach hook that is executed after each test.

    When called in the scope of a test file, runs after each test in the file. When called inside a test.describe([title, details, callback]) group, runs after 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 check whether the test succeeded or failed.

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

    Details

    When multiple afterEach 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.afterEach(async ({ page }) => {
    console.log(`Finished ${test.info().title} with status ${test.info().status}`);

    if (test.info().status !== test.info().expectedStatus)
    console.log(`Did not run as expected, ended up at ${page.url()}`);
    });

    test('my test', async ({ page }) => {
    // ...
    });

    Alternatively, you can delcare a hook with a title.

    // example.spec.ts
    test.afterEach('Status check', async ({ page }) => {
    if (test.info().status !== test.info().expectedStatus)
    console.log(`Did not run as expected, ended up at ${page.url()}`);
    });

    Parameters

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

    Returns void