Skip to content

Commit 9c9a590

Browse files
fix(screenshots): skip decoding and diffing identical images (#44336)
Co-authored-by: microsoft-playwright-automation[bot] <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com>
1 parent d70f422 commit 9c9a590

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎packages/utils/comparators.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ function compareImages(mimeType: string, actualBuffer: Buffer | string, expected
5959
if (!actualBuffer || !(actualBuffer instanceof Buffer))
6060
return { errorMessage: 'Actual result should be a Buffer.' };
6161
validateBuffer(expectedBuffer, mimeType);
62+
const comparator = options.comparator ?? 'pixelmatch';
63+
if (comparator !== 'pixelmatch' && comparator !== 'ssim-cie94')
64+
throw new Error(`Configuration specifies unknown comparator "${options.comparator}"`);
65+
if (actualBuffer.equals(expectedBuffer))
66+
return null;
6267

6368
const decode = (buffer: Buffer): ImageData => {
6469
if (mimeType === 'image/png')
@@ -69,6 +74,8 @@ function compareImages(mimeType: string, actualBuffer: Buffer | string, expected
6974
};
7075
let actual: ImageData = decode(actualBuffer);
7176
let expected: ImageData = decode(expectedBuffer);
77+
if (actual.width === expected.width && actual.height === expected.height && actual.data.equals(expected.data))
78+
return null;
7279
const size = { width: Math.max(expected.width, actual.width), height: Math.max(expected.height, actual.height) };
7380
let sizesMismatchError = '';
7481
if (expected.width !== actual.width || expected.height !== actual.height) {
@@ -78,18 +85,16 @@ function compareImages(mimeType: string, actualBuffer: Buffer | string, expected
7885
}
7986
const diff = new PNG({ width: size.width, height: size.height });
8087
let count;
81-
if (options.comparator === 'ssim-cie94') {
88+
if (comparator === 'ssim-cie94') {
8289
count = compare(expected.data, actual.data, diff.data, size.width, size.height, {
8390
// All ΔE* formulae are originally designed to have the difference of 1.0 stand for a "just noticeable difference" (JND).
8491
// See https://en.wikipedia.org/wiki/Color_difference#CIELAB_%CE%94E*
8592
maxColorDeltaE94: 1.0,
8693
});
87-
} else if ((options.comparator ?? 'pixelmatch') === 'pixelmatch') {
94+
} else {
8895
count = pixelmatch(expected.data, actual.data, diff.data, size.width, size.height, {
8996
threshold: options.threshold ?? 0.2,
9097
});
91-
} else {
92-
throw new Error(`Configuration specifies unknown comparator "${options.comparator}"`);
9398
}
9499

95100
const maxDiffPixels1 = options.maxDiffPixels;

‎tests/image_tools/unit.spec.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { test } from '../playwright-test/stable-test-runner';
17+
import { mock } from 'node:test';
18+
import { PNG } from 'playwright-core/lib/utilsBundle';
19+
import { test, expect } from '../playwright-test/stable-test-runner';
1820
import { utils } from '../../packages/playwright-core/lib/coreBundle';
1921

2022
import referenceSSIM from 'ssim.js';
@@ -23,6 +25,29 @@ import { randomPNG, assertEqual, grayChannel } from './utils';
2325
const { ssim, FastStats, ImageChannel, srgb2xyz, xyz2lab, colorDeltaE94 } = utils;
2426
type ImageChannel = InstanceType<typeof ImageChannel>;
2527

28+
test('image comparator should skip decoding identical buffers', {
29+
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44333' },
30+
}, () => {
31+
const image = randomPNG(10, 10, 239);
32+
const expected = PNG.sync.write(image, { filterType: 0 });
33+
const actual = Buffer.from(expected);
34+
const differentEncoding = PNG.sync.write(image, { filterType: 1 });
35+
expect(differentEncoding.equals(expected)).toBe(false);
36+
const comparator = utils.getComparator('image/png');
37+
const read = mock.method(PNG.sync, 'read');
38+
try {
39+
expect(comparator(actual, expected)).toBeNull();
40+
expect(read.mock.callCount()).toBe(0);
41+
expect(comparator(actual, expected, { comparator: 'ssim-cie94' })).toBeNull();
42+
expect(() => comparator(actual, expected, { comparator: 'unknown' })).toThrow('Configuration specifies unknown comparator "unknown"');
43+
expect(read.mock.callCount()).toBe(0);
44+
expect(comparator(differentEncoding, expected)).toBeNull();
45+
expect(read.mock.callCount()).toBe(2);
46+
} finally {
47+
read.mock.restore();
48+
}
49+
});
50+
2651
test('srgb to lab conversion should work', async () => {
2752
const srgb = [123, 81, 252];
2853
const [x, y, z] = srgb2xyz(srgb);

0 commit comments

Comments
 (0)