Rayrun

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

Answer

Sure, you can retry failing tests in @playwright/test. By default, this feature is turned off. To enable it, use the command npx playwright test --retries=3 to give failing tests three retry attempts.

You can also set this up in the playwright.config.ts file using defineConfig({retries: 3}).

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

export default defineConfig({
  retries: 3
});

Playwright categorizes tests as "passed", "flaky", or "failed". You can detect retries at runtime with testInfo.retry.

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

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

You can also specify retries for a specific group of tests or a single file with test.describe.configure().

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

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

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

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

If you have dependent tests that need to always run together and in order, use test.describe.serial().

Remember, it's usually better to make your Playwright Tests isolated so they can be efficiently run and retried independently. For more insights, check out this blog post on robust testing with Playwright.

References

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.