From d7648d390c496dbc4eedfbae6001c6e6c7f47565 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 4 Mar 2022 19:55:48 -0700 Subject: [PATCH] fix(toHaveScreenshot): round ratio up (#12540) This way the ratio value could be used right away in configuation. --- packages/playwright-core/src/utils/comparators.ts | 2 +- tests/playwright-test/to-have-screenshot.spec.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/playwright-core/src/utils/comparators.ts b/packages/playwright-core/src/utils/comparators.ts index 6cd73a047e..4a6319e00e 100644 --- a/packages/playwright-core/src/utils/comparators.ts +++ b/packages/playwright-core/src/utils/comparators.ts @@ -66,7 +66,7 @@ function compareImages(mimeType: string, actualBuffer: Buffer | string, expected maxDiffPixels = Math.min(maxDiffPixels1, maxDiffPixels2); else maxDiffPixels = maxDiffPixels1 ?? maxDiffPixels2 ?? 0; - const ratio = count / (expected.width * expected.height); + const ratio = Math.ceil(count / (expected.width * expected.height) * 100) / 100; return count > maxDiffPixels ? { errorMessage: `${count} pixels (ratio ${ratio.toFixed(2)} of all image pixels) are different`, diff: PNG.sync.write(diff), diff --git a/tests/playwright-test/to-have-screenshot.spec.ts b/tests/playwright-test/to-have-screenshot.spec.ts index ef78a1cfa2..02c559194e 100644 --- a/tests/playwright-test/to-have-screenshot.spec.ts +++ b/tests/playwright-test/to-have-screenshot.spec.ts @@ -245,7 +245,7 @@ test('should fail when screenshot is different size', async ({ runInlineTest }) test('should fail when screenshot is different pixels', async ({ runInlineTest }) => { const result = await runInlineTest({ ...files, - 'a.spec.js-snapshots/snapshot.png': blueImage, + 'a.spec.js-snapshots/snapshot.png': paintBlackPixels(whiteImage, 12345), 'a.spec.js': ` const { test } = require('./helper'); test('is a test', async ({ page }) => { @@ -255,9 +255,9 @@ test('should fail when screenshot is different pixels', async ({ runInlineTest } }); expect(result.exitCode).toBe(1); expect(result.output).toContain('Screenshot comparison failed'); - expect(result.output).toContain('921600 pixels'); + expect(result.output).toContain('12345 pixels'); expect(result.output).not.toContain('Call log'); - expect(result.output).toContain('ratio 1.00'); + expect(result.output).toContain('ratio 0.02'); expect(result.output).toContain('Expected:'); expect(result.output).toContain('Received:'); });