From e647f0420cda651eba5803359c408e367b0ba14a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 22 Nov 2021 10:06:20 -0800 Subject: [PATCH] docs: add more references to TestInfo.retry (#10472) --- docs/src/test-api/class-testinfo.md | 34 ++++++++++++++++++++++++ docs/src/test-retries-js.md | 22 +++++++++++++++ packages/playwright-test/types/test.d.ts | 18 +++++++++++++ 3 files changed, 74 insertions(+) diff --git a/docs/src/test-api/class-testinfo.md b/docs/src/test-api/class-testinfo.md index 2d0a3a2e13..551778e1a5 100644 --- a/docs/src/test-api/class-testinfo.md +++ b/docs/src/test-api/class-testinfo.md @@ -229,6 +229,40 @@ Specifies a unique repeat index when running in "repeat each" mode. This mode is Specifies the retry number when the test is retried after a failure. The first test run has [`property: TestInfo.retry`] equal to zero, the first retry has it equal to one, and so on. Learn more about [retries](./test-retries.md#retries). +```js js-flavor=js +const { test, expect } = require('@playwright/test'); + +test.beforeEach(async ({}, testInfo) => { + // You can access testInfo.retry in any hook or fixture. + if (testInfo.retry > 0) + console.log(`Retrying!`); +}); + +test('my test', async ({ page }, testInfo) => { + // Here we clear some server-side state when retrying. + if (testInfo.retry) + await cleanSomeCachesOnTheServer(); + // ... +}); +``` + +```js js-flavor=ts +import { test, expect } from '@playwright/test'; + +test.beforeEach(async ({}, testInfo) => { + // You can access testInfo.retry in any hook or fixture. + if (testInfo.retry > 0) + console.log(`Retrying!`); +}); + +test('my test', async ({ page }, testInfo) => { + // Here we clear some server-side state when retrying. + if (testInfo.retry) + await cleanSomeCachesOnTheServer(); + // ... +}); +``` + ## method: TestInfo.setTimeout Changes the timeout for the currently running test. Zero means no timeout. Learn more about [various timeouts](./test-timeouts.md). diff --git a/docs/src/test-retries-js.md b/docs/src/test-retries-js.md index a8bbbc42f3..7c150306e5 100644 --- a/docs/src/test-retries-js.md +++ b/docs/src/test-retries-js.md @@ -112,6 +112,28 @@ Running 3 tests using 1 worker 2 passed (4s) ``` +You can detect retries at runtime with [`property: TestInfo.retry`], which is accessible to any test, hook or fixture. Here is an example that clears some server-side state before a retry. + +```js js-flavor=js +const { test, expect } = require('@playwright/test'); + +test('my test', async ({ page }, testInfo) => { + if (testInfo.retry) + await cleanSomeCachesOnTheServer(); + // ... +}); +``` + +```js js-flavor=ts +import { test, expect } from '@playwright/test'; + +test('my test', async ({ page }, testInfo) => { + if (testInfo.retry) + await cleanSomeCachesOnTheServer(); + // ... +}); +``` + ## Serial mode Use [`method: Test.describe.serial`] to group dependent tests to ensure they will always run together and in order. If one of the tests fails, all subsequent tests are skipped. All tests in the group are retried together. diff --git a/packages/playwright-test/types/test.d.ts b/packages/playwright-test/types/test.d.ts index 60655dbda1..ccbaa9febb 100644 --- a/packages/playwright-test/types/test.d.ts +++ b/packages/playwright-test/types/test.d.ts @@ -1328,6 +1328,24 @@ export interface TestInfo { * Specifies the retry number when the test is retried after a failure. The first test run has * [testInfo.retry](https://playwright.dev/docs/api/class-testinfo#test-info-retry) equal to zero, the first retry has it * equal to one, and so on. Learn more about [retries](https://playwright.dev/docs/test-retries#retries). + * + * ```ts + * import { test, expect } from '@playwright/test'; + * + * test.beforeEach(async ({}, testInfo) => { + * // You can access testInfo.retry in any hook or fixture. + * if (testInfo.retry > 0) + * console.log(`Retrying!`); + * }); + * + * test('my test', async ({ page }, testInfo) => { + * // Here we clear some server-side state when retrying. + * if (testInfo.retry) + * await cleanSomeCachesOnTheServer(); + * // ... + * }); + * ``` + * */ retry: number; /**