-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
85 lines (74 loc) · 2.05 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
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
const Controller = require('happo-e2e/controller');
const pathToBrowserBuild = require.resolve('happo-e2e/browser.build.js');
const controller = new Controller();
async function lazyLoadBrowserBundle(page) {
if (
await page.evaluate(() => typeof window.happoTakeDOMSnapshot === 'undefined')
) {
await page.addScriptTag({ path: pathToBrowserBuild });
// Add timeout check for happoTakeDOMSnapshot
try {
await page.waitForFunction(
() => typeof window.happoTakeDOMSnapshot !== 'undefined',
{ timeout: 10000 },
);
} catch (error) {
throw new Error('Timed out waiting for happoTakeDOMSnapshot to be defined');
}
}
}
module.exports = {
async init(pageOrContext) {
if (pageOrContext) {
console.warn(
'[HAPPO] You no longer need to pass a page or context to happoPlaywright.init()',
);
}
await controller.init();
},
async finish() {
await controller.finish();
},
async screenshot(
page,
handleOrLocator,
{ component, variant, snapshotStrategy = 'hoist', ...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.',
);
}
await lazyLoadBrowserBundle(page);
const elementHandle = handleOrLocator.elementHandle
? await handleOrLocator.elementHandle()
: handleOrLocator;
const snapshot = await page.evaluate(
({ element, strategy }) =>
window.happoTakeDOMSnapshot({
doc: element.ownerDocument,
element,
strategy,
}),
{
element: elementHandle,
strategy: snapshotStrategy,
},
);
await controller.registerSnapshot({
...snapshot,
component,
variant,
...rest,
});
},
};