Skip to content

Commit 7f6bfd7

Browse files
committed
update: cy commands
1 parent 58fa285 commit 7f6bfd7

File tree

1 file changed

+124
-122
lines changed
  • bin/accessibility-automation/cypress

1 file changed

+124
-122
lines changed

bin/accessibility-automation/cypress/index.js

Lines changed: 124 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,116 @@ const performScan = (win, payloadToSend) =>
5757
}
5858
})
5959

60+
const getAccessibilityResultsSummary = (win) =>
61+
new Promise((resolve) => {
62+
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
63+
if (!isHttpOrHttps) {
64+
resolve();
65+
}
66+
67+
function findAccessibilityAutomationElement() {
68+
return win.document.querySelector("#accessibility-automation-element");
69+
}
70+
71+
function waitForScannerReadiness(retryCount = 30, retryInterval = 100) {
72+
return new Promise((resolve, reject) => {
73+
let count = 0;
74+
const intervalID = setInterval(() => {
75+
if (count > retryCount) {
76+
clearInterval(intervalID);
77+
reject(
78+
new Error(
79+
"Accessibility Automation Scanner is not ready on the page."
80+
)
81+
);
82+
} else if (findAccessibilityAutomationElement()) {
83+
clearInterval(intervalID);
84+
resolve("Scanner set");
85+
} else {
86+
count += 1;
87+
}
88+
}, retryInterval);
89+
});
90+
}
91+
92+
function getSummary() {
93+
function onReceiveSummary(event) {
94+
95+
win.removeEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
96+
resolve(event.detail);
97+
}
98+
99+
win.addEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
100+
const e = new CustomEvent("A11Y_GET_RESULTS_SUMMARY");
101+
win.dispatchEvent(e);
102+
}
103+
104+
if (findAccessibilityAutomationElement()) {
105+
getSummary();
106+
} else {
107+
waitForScannerReadiness()
108+
.then(getSummary)
109+
.catch((err) => {
110+
111+
});
112+
}
113+
})
114+
115+
const getAccessibilityResults = (win) =>
116+
new Promise((resolve) => {
117+
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
118+
if (!isHttpOrHttps) {
119+
resolve();
120+
}
121+
122+
function findAccessibilityAutomationElement() {
123+
return win.document.querySelector("#accessibility-automation-element");
124+
}
125+
126+
function waitForScannerReadiness(retryCount = 30, retryInterval = 100) {
127+
return new Promise((resolve, reject) => {
128+
let count = 0;
129+
const intervalID = setInterval(() => {
130+
if (count > retryCount) {
131+
clearInterval(intervalID);
132+
reject(
133+
new Error(
134+
"Accessibility Automation Scanner is not ready on the page."
135+
)
136+
);
137+
} else if (findAccessibilityAutomationElement()) {
138+
clearInterval(intervalID);
139+
resolve("Scanner set");
140+
} else {
141+
count += 1;
142+
}
143+
}, retryInterval);
144+
});
145+
}
146+
147+
function getResults() {
148+
function onReceivedResult(event) {
149+
150+
win.removeEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
151+
resolve(event.detail);
152+
}
153+
154+
win.addEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
155+
const e = new CustomEvent("A11Y_GET_RESULTS");
156+
win.dispatchEvent(e);
157+
}
158+
159+
if (findAccessibilityAutomationElement()) {
160+
getResults();
161+
} else {
162+
waitForScannerReadiness()
163+
.then(getResults)
164+
.catch((err) => {
165+
166+
});
167+
}
168+
});
169+
60170
const saveTestResults = (win, payloadToSend) =>
61171
new Promise( (resolve, reject) => {
62172
try {
@@ -224,74 +334,23 @@ afterEach(() => {
224334
});
225335
})
226336

337+
Cypress.Commands.add('perfromScan', () => {
338+
cy.window().then(async (win) => {
339+
await performScan(win);
340+
return await getAccessibilityResultsSummary(win);
341+
});
342+
})
343+
227344
Cypress.Commands.add('getAccessibilityResultsSummary', () => {
228345
try {
229346
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") {
230347
console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`);
231348
return
232349
}
233-
return new Promise(resolved => {
234-
cy.window().then((win) => {
235-
new Promise((resolve) => {
236-
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
237-
if (!isHttpOrHttps) {
238-
cy.log("Unable to retrieve accessibility result summary, Invalid URL.");
239-
resolve();
240-
}
241-
242-
function findAccessibilityAutomationElement() {
243-
return win.document.querySelector("#accessibility-automation-element");
244-
}
245-
246-
function waitForScannerReadiness(retryCount = 30, retryInterval = 100) {
247-
return new Promise((resolve, reject) => {
248-
let count = 0;
249-
const intervalID = setInterval(() => {
250-
if (count > retryCount) {
251-
clearInterval(intervalID);
252-
reject(
253-
new Error(
254-
"Accessibility Automation Scanner is not ready on the page."
255-
)
256-
);
257-
} else if (findAccessibilityAutomationElement()) {
258-
clearInterval(intervalID);
259-
resolve("Scanner set");
260-
} else {
261-
count += 1;
262-
}
263-
}, retryInterval);
264-
});
265-
}
266-
267-
function getSummary() {
268-
function onReceiveSummary(event) {
269-
cy.log("Received Summary");
270-
cy.log(event.detail);
271-
win.removeEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
272-
resolve(event.detail);
273-
}
274-
275-
win.addEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
276-
const e = new CustomEvent("A11Y_GET_RESULTS_SUMMARY");
277-
win.dispatchEvent(e);
278-
}
279-
280-
if (findAccessibilityAutomationElement()) {
281-
getSummary();
282-
} else {
283-
waitForScannerReadiness()
284-
.then(getSummary)
285-
.catch((err) => {
286-
cy.log(
287-
"Scanner is not ready on the page after multiple retries.",
288-
err
289-
);
290-
});
291-
}
292-
}).then(res => resolved(res)).finally(resolved);
350+
cy.window().then(async (win) => {
351+
await performScan(win);
352+
return await getAccessibilityResultsSummary(win);
293353
});
294-
})
295354
} catch {}
296355

297356
});
@@ -304,68 +363,11 @@ Cypress.Commands.add('getAccessibilityResults', () => {
304363
}
305364

306365
/* browserstack_accessibility_automation_script */
307-
return new Promise(resolved => {
308-
cy.window().then((win) => {
309-
new Promise((resolve) => {
310-
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
311-
if (!isHttpOrHttps) {
312-
cy.log("Unable to retrieve accessibility results, Invalid URL.");
313-
resolve();
314-
}
315-
316-
function findAccessibilityAutomationElement() {
317-
return win.document.querySelector("#accessibility-automation-element");
318-
}
319-
320-
function waitForScannerReadiness(retryCount = 30, retryInterval = 100) {
321-
return new Promise((resolve, reject) => {
322-
let count = 0;
323-
const intervalID = setInterval(() => {
324-
if (count > retryCount) {
325-
clearInterval(intervalID);
326-
reject(
327-
new Error(
328-
"Accessibility Automation Scanner is not ready on the page."
329-
)
330-
);
331-
} else if (findAccessibilityAutomationElement()) {
332-
clearInterval(intervalID);
333-
resolve("Scanner set");
334-
} else {
335-
count += 1;
336-
}
337-
}, retryInterval);
338-
});
339-
}
340-
341-
function getResults() {
342-
function onReceivedResult(event) {
343-
cy.log("Received Result");
344-
cy.log(event.detail);
345-
win.removeEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
346-
resolve(event.detail);
347-
}
348-
349-
win.addEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
350-
const e = new CustomEvent("A11Y_GET_RESULTS");
351-
win.dispatchEvent(e);
352-
}
353-
354-
if (findAccessibilityAutomationElement()) {
355-
getResults();
356-
} else {
357-
waitForScannerReadiness()
358-
.then(getResults)
359-
.catch((err) => {
360-
cy.log(
361-
"Scanner is not ready on the page after multiple retries.",
362-
err
363-
);
364-
});
365-
}
366-
}).then(res => resolved(res)).finally(resolved);
366+
367+
cy.window().then(async (win) => {
368+
await performScan(win);
369+
return await getAccessibilityResults(win);
367370
});
368-
})
369371

370372
} catch {}
371373

0 commit comments

Comments
 (0)