Skip to content

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

fix(screenshot): reject negative clip width/height (#41476)

Devin Roussoa1493506e283
diff --git a/packages/playwright-core/src/server/screenshotter.ts b/packages/playwright-core/src/server/screenshotter.ts
index c29e9e0b7c..41acde7934 100644
--- a/packages/playwright-core/src/server/screenshotter.ts
+++ b/packages/playwright-core/src/server/screenshotter.ts
@@ -349,7 +349,7 @@ function trimClipToSize(clip: types.Rect, size: types.Size): types.Rect {
y: Math.max(0, Math.min(clip.y + clip.height, size.height))
};
const result = { x: p1.x, y: p1.y, width: p2.x - p1.x, height: p2.y - p1.y };
- assert(result.width && result.height, 'Clipped area is either empty or outside the resulting image');
+ assert(result.width > 0 && result.height > 0, 'Clipped area is either empty or outside the resulting image');
return result;
}
@@ -376,8 +376,8 @@ export function validateScreenshotOptions(options: ScreenshotOptions): 'png' | '
assert(typeof options.clip.y === 'number', 'Expected options.clip.y to be a number but found ' + (typeof options.clip.y));
assert(typeof options.clip.width === 'number', 'Expected options.clip.width to be a number but found ' + (typeof options.clip.width));
assert(typeof options.clip.height === 'number', 'Expected options.clip.height to be a number but found ' + (typeof options.clip.height));
- assert(options.clip.width !== 0, 'Expected options.clip.width not to be 0.');
- assert(options.clip.height !== 0, 'Expected options.clip.height not to be 0.');
+ assert(options.clip.width > 0, 'Expected options.clip.width to be greater than 0.');
+ assert(options.clip.height > 0, 'Expected options.clip.height to be greater than 0.');
}
return format;
}
diff --git a/tests/page/page-screenshot.spec.ts b/tests/page/page-screenshot.spec.ts
index 12ccb4e693..3b73945b40 100644
--- a/tests/page/page-screenshot.spec.ts
+++ b/tests/page/page-screenshot.spec.ts
@@ -194,6 +194,20 @@ it.describe('page screenshot', () => {
expect(screenshotError.message).toContain('Clipped area is either empty or outside the resulting image');
});
+ it('should throw on a negative clip size', async ({ page, server }) => {
+ await page.setViewportSize({ width: 500, height: 500 });
+ await page.goto(server.PREFIX + '/grid.html');
+ const screenshotError = await page.screenshot({
+ clip: {
+ x: 50,
+ y: 50,
+ width: -100,
+ height: 100
+ }
+ }).catch(error => error);
+ expect(screenshotError.message).toContain('Expected options.clip.width to be greater than 0');
+ });
+
it('should run in parallel', async ({ page, server }) => {
await page.setViewportSize({ width: 500, height: 500 });
await page.goto(server.PREFIX + '/grid.html');
diff --git a/tests/playwright-test/to-have-screenshot.spec.ts b/tests/playwright-test/to-have-screenshot.spec.ts
index d99d293a7b..9a8945b014 100644
--- a/tests/playwright-test/to-have-screenshot.spec.ts
+++ b/tests/playwright-test/to-have-screenshot.spec.ts
@@ -233,7 +233,7 @@ test('should fail with proper error when unsupported argument is given', async (
`
}, { 'update-snapshots': true });
expect(result.exitCode).toBe(1);
- expect(result.output).toContain(`Expected options.clip.width not to be 0`);
+ expect(result.output).toContain(`Expected options.clip.width to be greater than 0`);
});
test('should have scale:css by default', async ({ runInlineTest }, testInfo) => {