@@ -4,192 +4,115 @@ const LambdatestLog = (message) => {
44 cy . task ( 'lambdatest_log' , message ) ;
55}
66
7- const commandsToWrap = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scroll' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
7+ const commandsToOverride = [
8+ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' ,
9+ 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scrollTo' ,
10+ 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin'
11+ ] ;
12+
13+ let currentWindow = null ;
14+ Cypress . Commands . add ( 'storeWindowObject' , ( ) => {
15+ cy . window ( ) . then ( win => {
16+ currentWindow = win ;
17+ } ) ;
18+ } ) ;
819
9- const setScanConfig = ( win , payload ) =>
10- new Promise ( async ( resolve , reject ) => {
20+ beforeEach ( ( ) => {
21+ cy . storeWindowObject ( ) ;
22+ } ) ;
23+
24+ const setScanConfig = ( win , payload ) => {
25+ return new Promise ( ( resolve , reject ) => {
1126 const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
12- if ( ! isHttpOrHttps ) {
13- resolve ( ) ;
14- }
27+ if ( ! isHttpOrHttps ) return resolve ( ) ;
28+
29+ console . log ( 'log' , "Accessibility setting scan config" ) ;
1530
16- function startScan ( ) {
17- console . log ( 'log' , "Accessibility setting scan config" )
1831 function onScanComplete ( event ) {
19- win . document . removeEventListener ( "automation-custom-event" , onScanComplete ) ;
20- console . log ( 'log' , "Recieved scan config data " + event . detail )
21- resolve ( event . detail ) ;
32+ win . document . removeEventListener ( "automation-custom-event" , onScanComplete ) ;
33+ console . log ( 'log' , "Received scan config data: " , event . detail ) ;
34+ resolve ( event . detail ) ;
2235 }
23-
24- win . document . addEventListener ( "automation-custom-event" , onScanComplete ) ;
36+
37+ win . document . addEventListener ( "automation-custom-event" , onScanComplete ) ;
2538 const e = new CustomEvent ( "accessibility-extension-custom-event" , { detail : payload } ) ;
2639 win . document . dispatchEvent ( e ) ;
27-
2840
29- setTimeout ( ( ) => {
30- resolve ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
31- } , 45000 ) ;
32- }
33- startScan ( ) ;
34-
35- } )
36-
37- const getScanData = ( win , payload ) =>
38- new Promise ( async ( resolve , reject ) => {
39- const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
40- if ( ! isHttpOrHttps ) {
41- resolve ( ) ;
42- }
43-
44-
45- function getSummary ( ) {
41+ setTimeout ( ( ) => {
42+ reject ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
43+ } , 45000 ) ;
44+ } ) ;
45+ } ;
46+
47+ const getScanData = ( win , payload ) => {
48+ return new Promise ( ( resolve , reject ) => {
49+ const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
50+ if ( ! isHttpOrHttps ) return resolve ( ) ;
51+
4652 function onReceiveSummary ( event ) {
47-
48- win . document . removeEventListener ( "automation-custom-event" , onReceiveSummary ) ;
49- resolve ( event . detail ) ;
53+ win . document . removeEventListener ( "automation-custom-event" , onReceiveSummary ) ;
54+ resolve ( event . detail ) ;
5055 }
51-
52-
56+
5357 win . document . addEventListener ( "automation-custom-event" , onReceiveSummary ) ;
5458 const e = new CustomEvent ( "accessibility-extension-custom-event" , { detail : payload } ) ;
5559 win . document . dispatchEvent ( e ) ;
5660
5761 setTimeout ( ( ) => {
58- resolve ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
62+ resolve ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
5963 } , 45000 ) ;
60-
61- }
62-
63-
64- getSummary ( ) ;
65-
66- } )
67-
68- function processAccessibilityReport ( win ) {
69- let wcagCriteriaValue = Cypress . env ( "WCAG_CRITERIA" ) || "wcag21a" ;
70- let bestPracticeValue = Cypress . env ( "BEST_PRACTICE" ) || false ;
71- let needsReviewValue = Cypress . env ( "NEEDS_REVIEW" ) || true ;
72- bestPracticeValue = bestPracticeValue == "true" ? true : false ;
73- needsReviewValue = needsReviewValue == "true" ? true : false ;
74- const payloadToSend = {
75- message : 'SET_CONFIG' ,
76- wcagCriteria : wcagCriteriaValue ,
77- bestPractice : bestPracticeValue ,
78- needsReview : needsReviewValue
79- }
80-
81- console . log ( 'log' , "payload to send " + payloadToSend ) ;
82- let testId = Cypress . env ( "TEST_ID" ) || ""
83-
84- const filePath = Cypress . env ( "ACCESSIBILITY_REPORT_PATH" ) || 'cypress/results/accessibilityReport_' + testId + '.json' ;
85-
86- cy . wrap ( setScanConfig ( win , payloadToSend ) , { timeout : 30000 } ) . then ( ( res ) => {
87- console . log ( 'logging config reponse' , res ) ;
88-
89- const payload = {
90- message : 'GET_LATEST_SCAN_DATA' ,
91- }
92-
93- cy . wrap ( getScanData ( win , payload ) , { timeout : 45000 } ) . then ( ( res ) => {
94- LambdatestLog ( 'log' , "scanning data " ) ;
95-
96-
97- cy . task ( 'initializeFile' , filePath ) . then ( ( filePath ) => {
98- cy . task ( 'readFileIfExists' , filePath , { log : true , timeout : 45000 } ) . then ( ( result ) => {
99- let resultsArray = [ { } ] ;
100- console . log ( 'logging report' , res ) ;
101- // If the file is not empty, parse the existing content
102- if ( result . exists && result . content ) {
103- try {
104- resultsArray = JSON . parse ( result . content ) ;
105- } catch ( e ) {
106- console . log ( "parsing error for content " , result . content )
107- console . log ( 'Error parsing JSON file:' , e ) ;
108- return ;
109- }
110- } else if ( ! result . exists ) {
111- console . log ( 'accessibility file does not exist' ) ;
112- }
113- if ( res ) {
114- console . log ( 'scanned data recieved is' , res . message ) ;
64+ } ) ;
65+ } ;
66+
67+ async function processAccessibilityReport ( url ) {
68+ try {
69+ let wcagCriteriaValue = Cypress . env ( "WCAG_CRITERIA" ) || "wcag21a" ;
70+ let bestPracticeValue = Cypress . env ( "BEST_PRACTICE" ) === "true" ;
71+ let needsReviewValue = Cypress . env ( "NEEDS_REVIEW" ) !== "false" ; // Default to true
72+
73+ const payloadToSend = {
74+ message : 'SET_CONFIG' ,
75+ wcagCriteria : wcagCriteriaValue ,
76+ bestPractice : bestPracticeValue ,
77+ needsReview : needsReviewValue
78+ } ;
79+
80+ console . log ( 'log' , "Payload to send: for url: " , payloadToSend , url ) ;
81+ try {
82+ let setResult = await setScanConfig ( currentWindow , payloadToSend ) ;
83+ console . log ( 'SET SCAN response:' , setResult ) ;
84+ } catch ( err ) {
85+ console . error ( "Error while setting scan" , err ) ;
11586 }
11687
117- if ( res && res . message == "GET_LATEST_SCAN_DATA" ) {
118- try {
119- // Append the new result
120- resultsArray . push ( res ) ;
121- console . log ( 'resultsarray logging' , resultsArray ) ;
122- } catch ( e ) {
123- console . log ( 'Error pushing issues to array:' , e ) ;
124- }
88+ let scanData ;
89+ try {
90+ const payload = { message : 'GET_LATEST_SCAN_DATA' } ;
91+ scanData = await getScanData ( currentWindow , payload ) ;
92+ LambdatestLog ( "LambdaTest Accessibility: Scanning URL" ) ;
93+ } catch ( err ) {
94+ console . error ( "GET SCAN:Error while setting scan" , err ) ;
12595 }
12696
127- // Write the updated content back to the file
128- cy . writeFile ( filePath , resultsArray , { log : true , timeout : 45000 } ) ;
129- } ) ;
130- } ) ;
131- } ) ;
97+ console . log ( "Logging response before sending to API:" , scanData ) ;
13298
133- } ) ;
99+ } catch ( error ) {
100+ LambdatestLog ( "ERROR" , error ) ;
101+ }
134102}
135103
136- Cypress . on ( 'command:start' , async ( command ) => {
137- if ( ! command || ! command . attributes ) return ;
138- if ( command . attributes . name == 'window' || command . attributes . name == 'then' || command . attributes . name == 'wrap' || command . attributes . name == 'wait' ) {
139- return ;
140- }
141-
142- if ( ! commandsToWrap . includes ( command . attributes . name ) ) return ;
143- let isAccessibilityLoaded = Cypress . env ( "ACCESSIBILITY" ) || false ;
144- if ( ! isAccessibilityLoaded ) {
145- console . log ( 'log' , "accessibility not enabled " + isAccessibilityLoaded ) ;
146- return ;
147- }
148-
149-
150- console . log ( 'log' , "debugging scan form command " + command . attributes . name ) ;
151-
152- cy . window ( ) . then ( ( win ) => {
153- processAccessibilityReport ( win ) ;
154- } ) ;
155- } )
156-
157- // Ensure scan runs AFTER visit completes
158- Cypress . Commands . overwrite ( 'visit' , ( originalFn , url , options ) => {
159- function getReportAfterVisiting ( ) {
160- originalFn ( url , options )
104+ commandsToOverride . forEach ( ( command ) => {
105+ Cypress . Commands . overwrite ( command , ( originalFn , url , options ) => {
161106 let isAccessibilityLoaded = Cypress . env ( "ACCESSIBILITY" ) || false ;
162107 if ( ! isAccessibilityLoaded ) {
163108 console . log ( 'log' , "Accessibility not enabled." ) ;
164- return ;
109+ return originalFn ( url , options ) ;
165110 }
166111
167- cy . window ( ) . then ( ( win ) => {
168- console . log ( 'log' , "Running accessibility scan after visit: " + url ) ;
169- processAccessibilityReport ( win ) ;
170- } ) ;
171- }
172-
173- return getReportAfterVisiting ( ) ;
174- } ) ;
175-
176-
177- Cypress . on ( 'command:end' , ( command ) => {
178-
179- return ;
180- } )
181112
113+ return originalFn ( url , options ) . then ( ( ) => {
114+ processAccessibilityReport ( url ) ;
115+ } )
182116
183- afterEach ( ( ) => {
184- console . log ( "after each hook" )
185- let isAccessibilityLoaded = Cypress . env ( "ACCESSIBILITY" ) || false ;
186- if ( ! isAccessibilityLoaded ) {
187- console . log ( 'log' , "accessibility not enabled " + isAccessibilityLoaded ) ;
188- return ;
189- }
190- cy . window ( ) . then ( ( win ) => {
191- processAccessibilityReport ( win ) ;
192- } )
193-
194-
195- } )
117+ } ) ;
118+ } ) ;
0 commit comments