-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (47 loc) · 1.41 KB
/
index.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
const Controller = require('happo-e2e/controller');
const pathToBrowserBuild = require.resolve('happo-e2e/browser.build.js');
const controller = new Controller();
module.exports = {
async init(contextOrPage) {
await contextOrPage.addInitScript({ path: pathToBrowserBuild });
await contextOrPage.addInitScript(`
window.addEventListener('load', () => {
window.happoTakeDOMSnapshot.init(window);
});
`);
await controller.init();
},
async finish() {
await controller.finish();
},
async screenshot(page, handleOrLocator, { component, variant, ...rest }) {
if (!controller.isActive()) {
return;
}
if (!component) {
throw new Error('Missing `component`');
}
if (!variant) {
throw new Error('Missing `variant`');
}
if (handleOrLocator instanceof Promise) {
throw new Error(
'handleOrLocator must be an element handle or a locator, received a promise. Please use `await` to resolve the handleOrLocator.',
);
}
const elementHandle = handleOrLocator.elementHandle
? await handleOrLocator.elementHandle()
: handleOrLocator;
const snapshot = await page.evaluate(
(element) =>
window.happoTakeDOMSnapshot({ doc: element.ownerDocument, element }),
elementHandle,
);
await controller.registerSnapshot({
...snapshot,
component,
variant,
...rest,
});
},
};