Skip to content

Commit 650d20e

Browse files
authored
Puppeteer: Removed pixelmatch dependency. (#32656)
1 parent 48f168d commit 650d20e

7 files changed

Lines changed: 48 additions & 21 deletions

File tree

-50.7 KB
Loading
-2.63 KB
Loading
-11.9 KB
Loading

package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
"jsdoc": "^4.0.5",
107107
"pngjs": "^7.0.0",
108108
"magic-string": "^0.30.0",
109-
"pixelmatch": "^7.0.0",
110109
"puppeteer": "^24.25.0",
111110
"qunit": "^2.19.4",
112111
"rollup": "^4.6.0",

test/e2e/image.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,52 @@ class Image {
102102

103103
}
104104

105+
compare( other, diff, threshold = 0.1 ) {
106+
107+
if ( this.width !== other.width || this.height !== other.height ) {
108+
109+
throw new Error( 'Image sizes do not match' );
110+
111+
}
112+
113+
const maxDelta = 255 * 255 * 3; // Max squared distance in RGB space
114+
let numDiffPixels = 0;
115+
116+
for ( let i = 0; i < this.data.length; i += 4 ) {
117+
118+
const dr = this.data[ i ] - other.data[ i ];
119+
const dg = this.data[ i + 1 ] - other.data[ i + 1 ];
120+
const db = this.data[ i + 2 ] - other.data[ i + 2 ];
121+
122+
// Squared Euclidean distance normalized to 0-1
123+
const delta = ( dr * dr + dg * dg + db * db ) / maxDelta;
124+
125+
if ( delta > threshold * threshold ) {
126+
127+
numDiffPixels ++;
128+
129+
// Mark difference in red
130+
diff.data[ i ] = 255;
131+
diff.data[ i + 1 ] = 0;
132+
diff.data[ i + 2 ] = 0;
133+
diff.data[ i + 3 ] = 255;
134+
135+
} else {
136+
137+
// Dim matching pixels
138+
diff.data[ i ] = this.data[ i ] * 0.2;
139+
diff.data[ i + 1 ] = this.data[ i + 1 ] * 0.2;
140+
diff.data[ i + 2 ] = this.data[ i + 2 ] * 0.2;
141+
diff.data[ i + 3 ] = 255;
142+
143+
}
144+
145+
}
146+
147+
return numDiffPixels;
148+
149+
}
150+
105151
async write( filepath, quality = 95 ) {
106152

107153
const rawImageData = {

test/e2e/puppeteer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import puppeteer from 'puppeteer';
2-
import pixelmatch from 'pixelmatch';
32
import { Image } from './image.js';
43
import * as fs from 'fs/promises';
54
import { createServer } from '../../utils/server.js';
@@ -524,16 +523,13 @@ async function makeAttempt( page, failedScreenshots, cleanPage, isMakeScreenshot
524523

525524
try {
526525

527-
numDifferentPixels = pixelmatch( expected.bitmap.data, actual.data, diff.bitmap.data, actual.width, actual.height, {
528-
threshold: pixelThreshold,
529-
alpha: 0.2
530-
} );
526+
numDifferentPixels = expected.compare( screenshot, diff, pixelThreshold );
531527

532528
} catch ( e ) {
533529

534530
await screenshot.write( `test/e2e/output-screenshots/${ file }-actual.jpg`, jpgQuality );
535531
await expected.write( `test/e2e/output-screenshots/${ file }-expected.jpg`, jpgQuality );
536-
throw new Error( `Image sizes does not match in file: ${ file }` );
532+
throw new Error( `Image sizes do not match in file: ${ file }` );
537533

538534
}
539535

0 commit comments

Comments
 (0)