This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (97 loc) · 2.63 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
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
import { resolve } from "node:path";
import mkdirp from "mkdirp";
const GLOBAL_STYLES = `
/* Hide carets */
* { caret-color: transparent !important; }
/* Hide scrollbars */
::-webkit-scrollbar {
display: none !important;
}
/* Generic hide */
[data-visual-test="transparent"] {
color: transparent !important;
font-family: monospace !important;
opacity: 0 !important;
}
[data-visual-test="removed"] {
display: none !important;
}
`;
/**
* Check if there is `[aria-busy="true"]` element on the page.
*/
async function ensureNoBusy() {
const checkIsVisible = (element) =>
Boolean(
element.offsetWidth ||
element.offsetHeight ||
element.getClientRects().length
);
return [...document.querySelectorAll('[aria-busy="true"]')].every(
(element) => !checkIsVisible(element)
);
}
/**
* Wait for all fonts to be loaded.
*/
function waitForFonts() {
return document.fonts.status === "loaded";
}
/**
* Wait for all images to be loaded.
*/
async function waitForImages() {
return Promise.all(
Array.from(document.images)
.filter((img) => !img.complete)
.map(
(img) =>
new Promise((resolve) => {
img.onload = img.onerror = resolve;
})
)
);
}
/**
* Disable spellcheck to avoid red underlines
*/
function disableSpellCheck() {
const query =
"[contenteditable]:not([contenteditable=false]):not([spellcheck=false]), input:not([spellcheck=false]), textarea:not([spellcheck=false])";
const inputs = document.querySelectorAll(query);
inputs.forEach((input) => input.setAttribute("spellcheck", "false"));
return true;
}
export async function argosScreenshot(
page,
name,
{ element = page, ...options } = {}
) {
if (!page) throw new Error("A Puppeteer `page` object is required.");
if (!name) throw new Error("The `name` argument is required.");
if (typeof element === "string") {
await page.waitForSelector(element);
element = await page.$(element);
}
const directory = resolve(process.cwd(), "screenshots/argos");
const [resolvedElement] = await Promise.all([
(async () => {
if (typeof element === "string") {
await page.waitForSelector(element);
return page.$(element);
}
return element;
})(),
mkdirp(directory),
page.addStyleTag({ content: GLOBAL_STYLES }),
page.waitForFunction(ensureNoBusy),
page.waitForFunction(waitForFonts),
page.waitForFunction(waitForImages),
page.waitForFunction(disableSpellCheck),
]);
await resolvedElement.screenshot({
path: resolve(directory, `${name}.png`),
type: "png",
...options,
});
}