Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(test-runner): support toHaveScreenshot timeout in expect config (#40720)

Pavel Feldmane1145c87c4c1
diff --git a/docs/src/test-api/class-testproject.md b/docs/src/test-api/class-testproject.md
index d301719c7c..ce55beb069 100644
--- a/docs/src/test-api/class-testproject.md
+++ b/docs/src/test-api/class-testproject.md
@@ -99,6 +99,7 @@ export default defineConfig({
- `scale` ?<[ScreenshotScale]<"css"|"device">> See [`option: Page.screenshot.scale`] in [`method: Page.screenshot`]. Defaults to `"css"`.
- `stylePath` ?<[string]|[Array]<[string]>> See [`option: Page.screenshot.style`] in [`method: Page.screenshot`].
- `pathTemplate` ?<[string]> A template controlling location of the screenshots. See [`property: TestProject.snapshotPathTemplate`] for details.
+ - `timeout` ?<[int]> Default timeout for [`method: PageAssertions.toHaveScreenshot#1`] in milliseconds, defaults to the global expect timeout. Setting to `0` disables the timeout.
- `toMatchAriaSnapshot` ?<[Object]> Configuration for the [`method: LocatorAssertions.toMatchAriaSnapshot#2`] method.
- `pathTemplate` ?<[string]> A template controlling location of the aria snapshots. See [`property: TestProject.snapshotPathTemplate`] for details.
- `children` ?<["contain" | "equal" | "deep-equal"]> Controls how children of the snapshot root are matched against the actual accessibility tree. This is equivalent to adding a `/children` property at the top of every aria snapshot template. Individual snapshots can override this by including an explicit `/children` property.
diff --git a/packages/playwright/src/matchers/expect.ts b/packages/playwright/src/matchers/expect.ts
index 2c754b94b2..ade895df50 100644
--- a/packages/playwright/src/matchers/expect.ts
+++ b/packages/playwright/src/matchers/expect.ts
@@ -128,6 +128,7 @@ export type ExpectConfig = {
scale?: 'css' | 'device';
stylePath?: string | string[];
pathTemplate?: string;
+ timeout?: number;
_comparator?: string;
};
toMatchSnapshot?: {
diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts
index 7f89b5166f..e075b65f98 100644
--- a/packages/playwright/src/matchers/toMatchSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchSnapshot.ts
@@ -40,6 +40,7 @@ type ToHaveScreenshotConfigOptions = ImageComparatorOptions & {
caret?: 'hide' | 'initial';
scale?: 'css' | 'device';
stylePath?: string | string[];
+ timeout?: number;
_comparator?: string;
};
@@ -54,7 +55,6 @@ type ToHaveScreenshotOptions = ToHaveScreenshotConfigOptions & {
mask?: Array<Locator>;
maskColor?: string;
omitBackground?: boolean;
- timeout?: number;
};
// Keep in sync with above (begin).
@@ -64,7 +64,6 @@ const NonConfigProperties: (keyof ToHaveScreenshotOptions)[] = [
'mask',
'maskColor',
'omitBackground',
- 'timeout',
];
// Keep in sync with above (end).
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 36efc5371c..1aa13bee41 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -237,6 +237,13 @@ interface TestProject<TestArgs = {}, WorkerArgs = {}> {
* for details.
*/
pathTemplate?: string;
+
+ /**
+ * Default timeout for
+ * [expect(page).toHaveScreenshot(name[, options])](https://playwright.dev/docs/api/class-pageassertions#page-assertions-to-have-screenshot-1)
+ * in milliseconds, defaults to the global expect timeout. Setting to `0` disables the timeout.
+ */
+ timeout?: number;
};
/**
diff --git a/tests/playwright-test/to-have-screenshot.spec.ts b/tests/playwright-test/to-have-screenshot.spec.ts
index 7c95c61aa8..d99d293a7b 100644
--- a/tests/playwright-test/to-have-screenshot.spec.ts
+++ b/tests/playwright-test/to-have-screenshot.spec.ts
@@ -124,6 +124,54 @@ test('should not retry serial mode suites with missing expectation errors', asyn
expect(result.exitCode).toBe(1);
});
+test('should respect toHaveScreenshot timeout from config', async ({ runInlineTest }, testInfo) => {
+ const infiniteAnimationURL = pathToFileURL(path.join(__dirname, '../assets/rotate-z.html'));
+ const result = await runInlineTest({
+ ...playwrightConfig({
+ expect: {
+ toHaveScreenshot: {
+ animations: 'allow',
+ timeout: 2000,
+ },
+ },
+ }),
+ 'a.spec.js': `
+ const { test, expect } = require('@playwright/test');
+ test('is a test', async ({ page }) => {
+ await page.goto('${infiniteAnimationURL}');
+ await expect(page).toHaveScreenshot();
+ });
+ `
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain(`Timeout 2000ms exceeded`);
+ expect(result.output).toContain(`Expect "toHaveScreenshot" with timeout 2000ms`);
+});
+
+test('should let toHaveScreenshot call options override config timeout', async ({ runInlineTest }, testInfo) => {
+ const infiniteAnimationURL = pathToFileURL(path.join(__dirname, '../assets/rotate-z.html'));
+ const result = await runInlineTest({
+ ...playwrightConfig({
+ expect: {
+ toHaveScreenshot: {
+ animations: 'allow',
+ timeout: 30000,
+ },
+ },
+ }),
+ 'a.spec.js': `
+ const { test, expect } = require('@playwright/test');
+ test('is a test', async ({ page }) => {
+ await page.goto('${infiniteAnimationURL}');
+ await expect(page).toHaveScreenshot({ timeout: 2000 });
+ });
+ `
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain(`Timeout 2000ms exceeded`);
+ expect(result.output).toContain(`Expect "toHaveScreenshot" with timeout 2000ms`);
+});
+
test.describe('expect config animations option', () => {
test('disabled', async ({ runInlineTest }, testInfo) => {
const cssTransitionURL = pathToFileURL(path.join(__dirname, '../assets/css-transition.html'));