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 5
/
Copy pathsupport.js
129 lines (111 loc) Β· 3.01 KB
/
support.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
import "cypress-wait-until";
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;
}
[data-test-no-radius] {
border-radius: 0 !important;
}
`;
/**
* Injected style to hide elements and fix unstable rendering.
*/
function injectStyles(document) {
const css = document.createElement("style");
css.type = "text/css";
css.textContent = GLOBAL_STYLES;
document.body.appendChild(css);
}
/**
* Check if an element is visible.
*/
function isVisible(element) {
return Boolean(
element.offsetWidth ||
element.offsetHeight ||
element.getClientRects().length
);
}
/**
* Wait until there is no `[aria-busy="true"]` element on the page.
*/
function waitUntilNoBusy() {
cy.waitUntil(() =>
cy.document().then((document) => {
const busy = Array.from(document.querySelectorAll('[aria-busy="true"]'));
return busy.every((element) => !isVisible(element));
})
);
}
/**
* Wait until all images are loaded.
*/
function waitForImagesLoading() {
cy.waitUntil(() =>
cy.document().then((document) => {
const allImages = Array.from(document.images);
allImages.forEach((img) => {
img.loading = "eager";
img.decoding = "sync";
});
return allImages.every((img) => img.complete && img.naturalWidth > 0);
})
);
}
/**
* Disable spellcheck on inputs, textareas, and contenteditable elements to avoid red underlines
*/
function disableSpellCheck() {
cy.document().then((document) => {
const query =
"[contenteditable]:not([contenteditable=false]):not([spellcheck=false]), input:not([spellcheck=false]), textarea:not([spellcheck=false])";
document.querySelectorAll(query).forEach((element) => {
element.setAttribute("spellcheck", "false");
});
});
return true;
}
Cypress.Commands.add(
"argosScreenshot",
{ prevSubject: ["optional", "element", "window", "document"] },
(subject, name, options = {}) => {
if (typeof name === "object" && name !== null) {
options = name;
name = null;
}
Cypress.log({
name: "argosScreenshot",
displayName: `Argos Screenshot`,
message: name,
});
// Inject styles
cy.document().then((doc) => injectStyles(doc));
// Wait until there is no `[aria-busy="true"]` element on the page.
waitUntilNoBusy();
// Wait for fonts to be loaded
cy.document().its("fonts.status").should("equal", "loaded");
// Wait for images to be loaded
waitForImagesLoading();
// Wait for images to be loaded
disableSpellCheck();
// Screenshot
cy.wrap(subject).screenshot(name, {
blackout: ['[data-visual-test="blackout"]'].concat(
options.blackout || []
),
...options,
});
}
);