Rayrun

How do I enable and configure retries for failing tests in Playwright?

Answer

To enable retries in Playwright, you can use the --retries flag when running tests or configure it in the playwright.config.ts file. For example, to give failing tests three retry attempts, run npx playwright test --retries=3.

In the configuration file, use defineConfig({retries: 3}) to set the number of retries. Playwright Test categorizes tests as "passed", "flaky", or "failed" based on their results.

You can access testInfo.retry at runtime to detect retries in any test, hook, or fixture. For instance, to clear server-side state before a retry, use:

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

test('my test', async ({ page }, testInfo) => {
  if (testInfo.retry) await cleanSomeCachesOnTheServer();
  // ...
});

To set retries for a specific group of tests or a single file, use test.describe.configure():

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

test.describe(() => {
  // All tests in this describe group will get 2 retry attempts.
  test.describe.configure({ retries: 2 });

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

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

For dependent tests that should always run together and in order, use test.describe.serial(). If one of these tests fails, subsequent ones will be skipped unless retries are enabled.

Thank you!
Was this helpful?
Still have questions?

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

TwitterGitHubLinkedIn
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 [email protected].