How do I use the afterAll hook in Playwright Test to run a specific test after all other tests have finished?


To run a specific test after all other tests in a test file have completed, use the test.afterAll() hook in @playwright/test. This hook executes once all tests in a file or group finish running. It's useful for cleanup tasks or running additional tests that depend on previous test results.

Define test.afterAll() at the top level of your test file, outside any test.describe() blocks. Inside the function passed to test.afterAll(), define your additional test using test() and perform any necessary actions.

Here's an example:

import { test } from '@playwright/test';

// Define your initial set of tests here

// Use test.afterAll() for an additional test
test.afterAll(async ({ page }) => {
  // Perform necessary actions
  await page.goto('https://ray.run');

  // Define your additional test
  test('additional test', async () => {
    // Perform assertions or other actions
    expect(await page.title()).toBe('Ray.run');

In this example, an additional test navigates to "https://ray.run" and checks if the title is "Ray.run". This test runs only after all other tests in the file finish.

Remember, if you add multiple afterAll hooks, they run in the order of their registration. Also, if multiple files have their own tests and hooks, each file executes independently with its own set of hooks.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.