forked from uswds/uswds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headless-chrome.js
executable file
·111 lines (91 loc) · 3.25 KB
/
headless-chrome.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
const { fractalLoad } = require('./delayed-root-suite');
const VisualRegressionTester = require('./visual-regression-tester');
const ChromeFractalTester = require('./chrome-fractal-tester');
const axeTester = require('./axe-tester');
class Device {
constructor (name, metrics) {
this.name = name;
this.metrics = Object.assign({
deviceScaleFactor: 1,
mobile: false,
fitWindow: false,
}, metrics);
}
get description () {
const m = this.metrics;
const parts = [ `${m.width}x${m.height}` ];
if (m.deviceScaleFactor !== 1) parts.push(`@ ${m.deviceScaleFactor}x`);
if (m.mobile) parts.push('mobile');
return `${this.name} (${parts.join(' ')})`;
}
}
const SKIP_COMPONENTS = [
// Any components that need to be temporarily skipped can be put
// here. They will be regarded as a "pending test" by Mocha.
'layout--docs-inner',
'layout--landing-inner',
];
const DEVICES = [
new Device('small-desktop', {
width: 412,
height: 732,
}),
new Device('large-desktop', {
width: 1280,
height: 732,
}),
];
fractalLoad.then(function runFractalTester() {
const chromeFractalTester = new ChromeFractalTester();
const { handles } = chromeFractalTester;
describe('fractal component', function () {
this.timeout(20000);
before('setup ChromeFractalTester', chromeFractalTester.setup);
after('teardown ChromeFractalTester', chromeFractalTester.teardown);
if (process.env.ENABLE_SCREENSHOTS) {
if (process.env.UPDATE_GOLDEN_SCREENSHOTS) {
VisualRegressionTester.cleanSync(handles, DEVICES);
}
after('create visual regression testing metadata',
() => VisualRegressionTester.writeMetadata(handles, DEVICES));
}
handles.forEach((handle) => {
let cdp;
describe(`"${handle}"`, () => {
if (SKIP_COMPONENTS.includes(handle)) {
it('skipping for now. TODO: fix this test!');
return;
}
before('init chrome devtools protocol', () => {
return chromeFractalTester.createChromeDevtoolsProtocol()
.then(client => { cdp = client; });
});
before(`load fractal component in chrome`, function () {
this.timeout(20000);
return chromeFractalTester.loadFractalPreview(cdp, handle);
});
before('inject aXe', () => axeTester.load(cdp));
after('shutdown chrome devtools protocol', () => cdp.close());
DEVICES.forEach(device => {
describe(`on ${device.description}`, () => {
before('set device metrics', () => {
return cdp.Emulation.setDeviceMetricsOverride(device.metrics);
});
it('has no aXe violations', () => axeTester.run(cdp));
if (process.env.ENABLE_SCREENSHOTS) {
const vrt = new VisualRegressionTester({ handle, device });
if (vrt.doesGoldenFileExist()) {
it('matches golden screenshot',
() => vrt.screenshot(cdp)
.then(vrt.ensureMatchesGoldenFile));
} else {
it('is the new golden screenshot',
() => vrt.screenshot(cdp).then(vrt.saveToGoldenFile));
}
}
});
});
});
});
});
});