Rayrun

What is the process to enable retries for failing tests in Playwright?

Answer

Enabling Test Retries in Playwright

To enable test retries in Playwright, use the command npx playwright test --retries=3. This will give failing tests three retry attempts. By default, Playwright does not retry failing tests.

You can also set up retries in the playwright.config.ts configuration file. Use defineConfig({retries: 3}) to specify the number of retries.

Playwright categorizes tests as "passed", "flaky", or "failed". A passed test is one that passed on the first run. A flaky test failed initially but passed on retry. A failed test is one that failed on all runs, including retries.

Detecting Retries at Runtime

You can detect retries at runtime using testInfo.retry. This is accessible to any test, hook, or fixture. Here's an example:

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

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

Specifying Retries for Specific Tests

To specify retries for a specific group of tests or a single file, use test.describe.configure(). Here's how:

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

test.describe(() => {
  test.describe.configure({ retries: 2 });

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

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

Lastly, use test.describe.serial() to group dependent tests. If one test fails, all subsequent ones will be skipped unless retries are enabled.

For more on handling flaky tests, check out Mastering the Art of Detecting and Handling Flaky Tests in Playwright.

Thank you!
Was this helpful?
Still have questions?

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

Related Questions

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 luc@ray.run.