|
1 | 1 | /* Event listeners + custom commands for Cypress */
|
2 | 2 |
|
3 | 3 | Cypress.on('test:before:run', () => {
|
4 |
| - if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return |
5 |
| - const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH") |
6 |
| - |
7 |
| - if (extensionPath !== undefined) { |
8 |
| - new Promise((resolve, reject) => { |
9 |
| - window.parent.addEventListener('A11Y_TAP_STARTED', () => { |
10 |
| - resolve("A11Y_TAP_STARTED"); |
11 |
| - }); |
12 |
| - const e = new CustomEvent('A11Y_FORCE_START'); |
13 |
| - window.parent.dispatchEvent(e); |
14 |
| - }) |
15 |
| - } |
16 |
| -}) |
| 4 | + try { |
| 5 | + if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return |
| 6 | + const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH") |
| 7 | + |
| 8 | + if (extensionPath !== undefined) { |
| 9 | + new Promise((resolve, reject) => { |
| 10 | + window.parent.addEventListener('A11Y_TAP_STARTED', () => { |
| 11 | + resolve("A11Y_TAP_STARTED"); |
| 12 | + }); |
| 13 | + const e = new CustomEvent('A11Y_FORCE_START'); |
| 14 | + window.parent.dispatchEvent(e); |
| 15 | + }) |
| 16 | + } |
| 17 | + } catch {} |
| 18 | + |
| 19 | +}); |
17 | 20 |
|
18 | 21 | Cypress.on('test:after:run', (attributes, runnable) => {
|
19 |
| - if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return |
20 |
| - const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH") |
21 |
| - const isHeaded = Cypress.browser.isHeaded; |
22 |
| - if (isHeaded && extensionPath !== undefined) { |
| 22 | + try { |
| 23 | + if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return |
| 24 | + const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH") |
| 25 | + const isHeaded = Cypress.browser.isHeaded; |
| 26 | + if (isHeaded && extensionPath !== undefined) { |
23 | 27 |
|
24 |
| - let shouldScanTestForAccessibility = true; |
25 |
| - if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY") || Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) { |
| 28 | + let shouldScanTestForAccessibility = true; |
| 29 | + if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY") || Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) { |
26 | 30 |
|
27 |
| - try { |
28 |
| - let includeTagArray = Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY").split(";") |
29 |
| - let excludeTagArray = Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY").split(";") |
| 31 | + try { |
| 32 | + let includeTagArray = []; |
| 33 | + let excludeTagArray = []; |
| 34 | + if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY")) { |
| 35 | + includeTagArray = Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY").split(";") |
| 36 | + } |
| 37 | + if (Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) { |
| 38 | + excludeTagArray = Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY").split(";") |
| 39 | + } |
30 | 40 |
|
31 |
| - const fullTestName = attributes.title; |
32 |
| - const excluded = excludeTagArray.some((exclude) => fullTestName.includes(exclude)); |
33 |
| - const included = includeTagArray.length === 0 || includeTags.some((include) => fullTestName.includes(include)); |
34 |
| - shouldScanTestForAccessibility = !excluded && included; |
35 |
| - } catch (error){ |
36 |
| - console.log("Error while validating test case for accessibility before scanning. Error : ", error); |
| 41 | + const fullTestName = attributes.title; |
| 42 | + const excluded = excludeTagArray.some((exclude) => fullTestName.includes(exclude)); |
| 43 | + const included = includeTagArray.length === 0 || includeTags.some((include) => fullTestName.includes(include)); |
| 44 | + shouldScanTestForAccessibility = !excluded && included; |
| 45 | + } catch (error){ |
| 46 | + console.log("Error while validating test case for accessibility before scanning. Error : ", error); |
| 47 | + } |
37 | 48 | }
|
38 |
| - } |
39 |
| - let os_data; |
40 |
| - if (Cypress.env("OS")) { |
41 |
| - os_data = Cypress.env("OS"); |
42 |
| - } else { |
43 |
| - os_data = Cypress.platform === 'linux' ? 'mac' : "win" |
44 |
| - } |
45 |
| - const dataForExtension = { |
46 |
| - "saveResults": shouldScanTestForAccessibility, |
47 |
| - "testDetails": { |
48 |
| - "name": attributes.title, |
49 |
| - "testRunId": '5058', // variable not consumed, shouldn't matter what we send |
50 |
| - "filePath": attributes.invocationDetails.relativeFile, |
51 |
| - "scopeList": [ |
52 |
| - attributes.invocationDetails.relativeFile, |
53 |
| - attributes.title |
54 |
| - ] |
55 |
| - }, |
56 |
| - "platform": { |
57 |
| - "os_name": os_data, |
58 |
| - "os_version": Cypress.env("OS_VERSION"), |
59 |
| - "browser_name": Cypress.browser.name, |
60 |
| - "browser_version": Cypress.browser.version |
| 49 | + let os_data; |
| 50 | + if (Cypress.env("OS")) { |
| 51 | + os_data = Cypress.env("OS"); |
| 52 | + } else { |
| 53 | + os_data = Cypress.platform === 'linux' ? 'mac' : "win" |
61 | 54 | }
|
62 |
| - }; |
63 |
| - return new Promise((resolve, reject) => { |
64 |
| - if (dataForExtension.saveResults) { |
65 |
| - window.parent.addEventListener('A11Y_TAP_TRANSPORTER', (event) => { |
66 |
| - resolve(event.detail); |
67 |
| - }); |
68 |
| - } |
69 |
| - const e = new CustomEvent('A11Y_TEST_END', {detail: dataForExtension}); |
70 |
| - window.parent.dispatchEvent(e); |
71 |
| - if (dataForExtension.saveResults !== true ) |
72 |
| - resolve(); |
73 |
| - }); |
74 |
| - } |
| 55 | + const dataForExtension = { |
| 56 | + "saveResults": shouldScanTestForAccessibility, |
| 57 | + "testDetails": { |
| 58 | + "name": attributes.title, |
| 59 | + "testRunId": '5058', // variable not consumed, shouldn't matter what we send |
| 60 | + "filePath": attributes.invocationDetails.relativeFile, |
| 61 | + "scopeList": [ |
| 62 | + attributes.invocationDetails.relativeFile, |
| 63 | + attributes.title |
| 64 | + ] |
| 65 | + }, |
| 66 | + "platform": { |
| 67 | + "os_name": os_data, |
| 68 | + "os_version": Cypress.env("OS_VERSION"), |
| 69 | + "browser_name": Cypress.browser.name, |
| 70 | + "browser_version": Cypress.browser.version |
| 71 | + } |
| 72 | + }; |
| 73 | + return new Promise((resolve, reject) => { |
| 74 | + if (dataForExtension.saveResults) { |
| 75 | + window.parent.addEventListener('A11Y_TAP_TRANSPORTER', (event) => { |
| 76 | + resolve(event.detail); |
| 77 | + }); |
| 78 | + } |
| 79 | + const e = new CustomEvent('A11Y_TEST_END', {detail: dataForExtension}); |
| 80 | + window.parent.dispatchEvent(e); |
| 81 | + if (dataForExtension.saveResults !== true ) |
| 82 | + resolve(); |
| 83 | + }); |
| 84 | + } |
75 | 85 |
|
| 86 | + } catch {} |
76 | 87 | });
|
77 | 88 |
|
78 | 89 | Cypress.Commands.add('getAccessibilityResultsSummary', () => {
|
79 |
| - if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") { |
80 |
| - console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`); |
81 |
| - return |
82 |
| - } |
83 |
| - return new Promise(function (resolve, reject) { |
84 |
| - try{ |
85 |
| - const e = new CustomEvent('A11Y_TAP_GET_RESULTS_SUMMARY'); |
86 |
| - const fn = function (event) { |
87 |
| - window.parent.removeEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn); |
88 |
| - resolve(event.detail.summary); |
89 |
| - }; |
90 |
| - window.parent.addEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn); |
91 |
| - window.parent.dispatchEvent(e); |
92 |
| - } catch (err) { |
93 |
| - console.log("No accessibility results summary was found."); |
94 |
| - reject(err); |
| 90 | + try { |
| 91 | + if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") { |
| 92 | + console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`); |
| 93 | + return |
95 | 94 | }
|
96 |
| - }); |
| 95 | + return new Promise(function (resolve, reject) { |
| 96 | + try{ |
| 97 | + const e = new CustomEvent('A11Y_TAP_GET_RESULTS_SUMMARY'); |
| 98 | + const fn = function (event) { |
| 99 | + window.parent.removeEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn); |
| 100 | + resolve(event.detail.summary); |
| 101 | + }; |
| 102 | + window.parent.addEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn); |
| 103 | + window.parent.dispatchEvent(e); |
| 104 | + } catch (err) { |
| 105 | + console.log("No accessibility results summary was found."); |
| 106 | + reject(err); |
| 107 | + } |
| 108 | + }); |
| 109 | + } catch {} |
| 110 | + |
97 | 111 | });
|
98 | 112 |
|
99 | 113 | Cypress.Commands.add('getAccessibilityResults', () => {
|
100 |
| - if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") { |
101 |
| - console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`); |
102 |
| - return |
103 |
| - } |
104 |
| - return new Promise(function (resolve, reject) { |
105 |
| - try{ |
106 |
| - const e = new CustomEvent('A11Y_TAP_GET_RESULTS'); |
107 |
| - const fn = function (event) { |
108 |
| - window.parent.removeEventListener('A11Y_RESULTS_RESPONSE', fn); |
109 |
| - resolve(event.detail.summary); |
110 |
| - }; |
111 |
| - window.parent.addEventListener('A11Y_RESULTS_RESPONSE', fn); |
112 |
| - window.parent.dispatchEvent(e); |
113 |
| - } catch (err) { |
114 |
| - console.log("No accessibility results were found."); |
115 |
| - reject(err); |
| 114 | + try { |
| 115 | + if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") { |
| 116 | + console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`); |
| 117 | + return |
116 | 118 | }
|
117 |
| - }); |
| 119 | + return new Promise(function (resolve, reject) { |
| 120 | + try{ |
| 121 | + const e = new CustomEvent('A11Y_TAP_GET_RESULTS'); |
| 122 | + const fn = function (event) { |
| 123 | + window.parent.removeEventListener('A11Y_RESULTS_RESPONSE', fn); |
| 124 | + resolve(event.detail.summary); |
| 125 | + }; |
| 126 | + window.parent.addEventListener('A11Y_RESULTS_RESPONSE', fn); |
| 127 | + window.parent.dispatchEvent(e); |
| 128 | + } catch (err) { |
| 129 | + console.log("No accessibility results were found."); |
| 130 | + reject(err); |
| 131 | + } |
| 132 | + }); |
| 133 | + } catch {} |
| 134 | + |
118 | 135 | });
|
119 | 136 |
|
0 commit comments