This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
visual_regression.js
139 lines (117 loc) · 3.87 KB
/
visual_regression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import bluebird, {
fromNode,
promisify,
} from 'bluebird';
import Handlebars from 'handlebars';
import fs from 'fs';
import path from 'path';
import imageDiff from 'image-diff';
import mkdirp from 'mkdirp';
import moment from 'moment';
import SimpleGit from 'simple-git';
const readDirAsync = promisify(fs.readdir);
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
Handlebars.registerHelper('lte', function lessThanEquals(value, threshold, options) {
if (value <= threshold) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('gte', function greaterThanEquals(value, threshold, options) {
if (value >= threshold) {
return options.fn(this);
}
return options.inverse(this);
});
async function buildGallery(comparisons) {
const simpleGit = new SimpleGit();
const asyncBranch = promisify(simpleGit.branch, simpleGit);
const branch = await asyncBranch();
const template = Handlebars.compile(await readFileAsync(
path.resolve('./utilities/templates/visual_regression_gallery.handlebars')
, 'utf8'));
const html = template({
date: moment().format('MMMM Do YYYY, h:mm:ss a'),
branch: branch.current,
hiddenThreshold: 0,
warningThreshold: 0.03,
comparisons,
});
return writeFileAsync(
path.resolve('./test/functional/screenshots/visual_regression_gallery.html'),
html
);
}
async function compareScreenshots() {
const SCREENSHOTS_DIR = 'test/functional/screenshots';
const BASELINE_SCREENSHOTS_DIR = path.resolve(SCREENSHOTS_DIR, 'baseline');
const DIFF_SCREENSHOTS_DIR = path.resolve(SCREENSHOTS_DIR, 'diff');
const SESSION_SCREENSHOTS_DIR = path.resolve(SCREENSHOTS_DIR, 'session');
// We don't need to create the baseline dir because it's committed.
mkdirp.sync(DIFF_SCREENSHOTS_DIR);
mkdirp.sync(SESSION_SCREENSHOTS_DIR);
const files = await readDirAsync(SESSION_SCREENSHOTS_DIR);
const screenshots = files.filter(file => file.indexOf('.png') !== -1);
// We'll use this data to build a screenshot gallery in HTML.
return await bluebird.map(screenshots, async screenshot => {
// We're going to load image data and cache it in this object.
const comparison = {
name: screenshot,
change: undefined,
percentage: undefined,
imageData: {
session: undefined,
baseline: undefined,
diff: undefined,
}
};
const sessionImagePath = path.resolve(
SESSION_SCREENSHOTS_DIR,
screenshot
);
const baselineImagePath = path.resolve(
BASELINE_SCREENSHOTS_DIR,
screenshot
);
const diffImagePath = path.resolve(
DIFF_SCREENSHOTS_DIR,
screenshot
);
// Diff the images asynchronously.
const diffResult = await fromNode((cb) => {
imageDiff.getFullResult({
actualImage: sessionImagePath,
expectedImage: baselineImagePath,
diffImage: diffImagePath,
shadow: true,
}, cb);
});
const change = diffResult.percentage;
const changePercentage = (change * 100).toFixed(2);
console.log(`(${changePercentage}%) ${screenshot}`);
comparison.percentage = changePercentage;
comparison.change = change;
// Once the images have been diffed, we can load and store the image data.
comparison.imageData.session =
await readFileAsync(sessionImagePath, 'base64');
comparison.imageData.baseline =
await readFileAsync(baselineImagePath, 'base64');
comparison.imageData.diff =
await readFileAsync(diffImagePath, 'base64');
return comparison;
});
}
module.exports = {
run: done => {
compareScreenshots().then(screenshotComparisons => {
// Once all of the data has been loaded, we can build the gallery.
buildGallery(screenshotComparisons).then(() => {
done();
});
}, error => {
console.error(error);
done(false);
});
}
};