Skip to content

Commit 2765d68

Browse files
committed
improve screenshot diff stuff
full screen screenshots were sometimes just blank
1 parent 78e7424 commit 2765d68

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

e2e/screenshot.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function screenshotPathname(pathname: string) {
2222
await page.waitForFunction(waitForDocusaurusHydration);
2323
await page.addStyleTag({content: stylesheet});
2424
const spath = `./screenshots/${pathnameToArgosName(pathname)}.png`
25-
await page.screenshot({ path: spath, fullPage: true });
25+
await page.screenshot({ path: spath });
2626
});
2727
}
2828

scripts/image-diff.ts

+29-16
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,29 @@ import fs from "node:fs"
77
import os from "node:os"
88
import path from "node:path"
99
import { PNG } from "pngjs"
10-
import pixelmatch from "pixelmatch"
10+
import pixelmatch from "pixelmatch"
1111
import { asyncFolderWalker } from 'async-folder-walker'
1212

13-
const basePath1 = os.homedir() + '/Downloads/screenshots-main/'
14-
const basePath2 = os.homedir() + '/Downloads/screenshots-3/'
13+
const basePath1 = '../screenshots-main'
14+
const basePath2 = "../screenshots/"
1515

1616
iterateFiles()
1717

18-
async function iterateFiles () {
18+
async function iterateFiles() {
1919
const walker = asyncFolderWalker([basePath1])
2020
let tmpdir = ''
2121

22+
try {
23+
tmpdir = os.tmpdir()
24+
console.log(tmpdir)
25+
fs.mkdtempSync(tmpdir)
26+
tmpdir = path.join(tmpdir, "diffs/")
27+
fs.mkdirSync(tmpdir)
28+
} catch (e) {
29+
console.log(e.message)
30+
}
31+
32+
2233
for await (const file of walker) {
2334
if (typeof file == 'string') {
2435
if (file.includes(".png")) {
@@ -27,19 +38,21 @@ async function iterateFiles () {
2738
const otherFile = file.replace(basePath1, basePath2)
2839
const img2 = PNG.sync.read(fs.readFileSync(otherFile));
2940

30-
const {width, height} = img1;
31-
const diff = new PNG({width, height});
32-
const result = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1})
33-
if (result > 0) {
34-
console.log(result, file, otherFile)
35-
if (!tmpdir) {
36-
tmpdir = path.join(os.tmpdir(), 'diffs-')
37-
fs.mkdtempSync(tmpdir)
41+
const { width, height } = img1;
42+
const diff = new PNG({ width, height });
43+
try {
44+
const result = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 })
45+
if (result > 0) {
46+
console.log(result, file, otherFile)
47+
48+
const dest = tmpdir + path.parse(file).base
49+
console.log("diff image saved to:")
50+
console.log(dest)
51+
fs.writeFileSync(dest, PNG.sync.write(diff));
3852
}
39-
40-
const dest = tmpdir + path.parse(file).base
41-
console.log("diff image saved to:")
42-
console.log(dest)
53+
} catch (e) {
54+
console.log(e.message, file)
55+
const dest = tmpdir + "err-" + path.parse(file).base
4356
fs.writeFileSync(dest, PNG.sync.write(diff));
4457
}
4558
}

0 commit comments

Comments
 (0)