@@ -10,6 +10,8 @@ const commandsToOverride = [
1010 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin'
1111] ;
1212
13+ const commandsToWrap = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scroll' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
14+
1315let currentWindow = null ;
1416Cypress . Commands . add ( 'storeWindowObject' , ( ) => {
1517 cy . window ( ) . then ( win => {
@@ -148,20 +150,116 @@ async function processAccessibilityReport(url) {
148150 }
149151}
150152
151- commandsToOverride . forEach ( ( command ) => {
152- Cypress . Commands . overwrite ( command , ( originalFn , url , options ) => {
153+ function oldprocessAccessibilityReport ( win ) {
154+ let wcagCriteriaValue = Cypress . env ( "WCAG_CRITERIA" ) || "wcag21a" ;
155+ let bestPracticeValue = Cypress . env ( "BEST_PRACTICE" ) || false ;
156+ let needsReviewValue = Cypress . env ( "NEEDS_REVIEW" ) || true ;
157+ bestPracticeValue = bestPracticeValue == "true" ? true : false ;
158+ needsReviewValue = needsReviewValue == "true" ? true : false ;
159+ const payloadToSend = {
160+ message : 'SET_CONFIG' ,
161+ wcagCriteria : wcagCriteriaValue ,
162+ bestPractice : bestPracticeValue ,
163+ needsReview : needsReviewValue
164+ }
165+
166+ console . log ( 'log' , "payload to send " + payloadToSend ) ;
167+ let testId = Cypress . env ( "TEST_ID" ) || ""
168+
169+ const filePath = Cypress . env ( "ACCESSIBILITY_REPORT_PATH" ) || 'cypress/results/accessibilityReport_' + testId + '.json' ;
170+
171+ cy . wrap ( setScanConfig ( win , payloadToSend ) , { timeout : 30000 } ) . then ( ( res ) => {
172+ console . log ( 'logging config reponse' , res ) ;
173+
174+ const payload = {
175+ message : 'GET_LATEST_SCAN_DATA' ,
176+ }
177+
178+ cy . wrap ( getScanData ( win , payload ) , { timeout : 45000 } ) . then ( ( res ) => {
179+ LambdatestLog ( 'log' , "scanning data " ) ;
180+
181+
182+ cy . task ( 'initializeFile' , filePath ) . then ( ( filePath ) => {
183+ cy . task ( 'readFileIfExists' , filePath , { log : true , timeout : 45000 } ) . then ( ( result ) => {
184+ let resultsArray = [ { } ] ;
185+ console . log ( 'logging report' , res ) ;
186+ // If the file is not empty, parse the existing content
187+ if ( result . exists && result . content ) {
188+ try {
189+ resultsArray = JSON . parse ( result . content ) ;
190+ } catch ( e ) {
191+ console . log ( "parsing error for content " , result . content )
192+ console . log ( 'Error parsing JSON file:' , e ) ;
193+ return ;
194+ }
195+ } else if ( ! result . exists ) {
196+ console . log ( 'accessibility file does not exist' ) ;
197+ }
198+ if ( res ) {
199+ console . log ( 'scanned data recieved is' , res . message ) ;
200+ }
201+
202+ if ( res && res . message == "GET_LATEST_SCAN_DATA" ) {
203+ try {
204+ // Append the new result
205+ resultsArray . push ( res ) ;
206+ console . log ( 'resultsarray logging' , resultsArray ) ;
207+ } catch ( e ) {
208+ console . log ( 'Error pushing issues to array:' , e ) ;
209+ }
210+ }
211+
212+ // Write the updated content back to the file
213+ cy . writeFile ( filePath , resultsArray , { log : true , timeout : 45000 } ) ;
214+ } ) ;
215+ } ) ;
216+ } ) ;
217+
218+ } ) ;
219+ }
220+
221+ const overRideCommands = Cypress . env ( "ACCESSIBILITY_OVERIDE_COMMANDS" ) || false ;
222+ if ( overRideCommands ) {
223+ commandsToOverride . forEach ( ( command ) => {
224+ Cypress . Commands . overwrite ( command , ( originalFn , url , options ) => {
225+ let isAccessibilityLoaded = Cypress . env ( "ACCESSIBILITY" ) || false ;
226+ if ( ! isAccessibilityLoaded ) {
227+ console . log ( 'log' , "Accessibility not enabled." ) ;
228+ return originalFn ( url , options ) ;
229+ }
230+
231+
232+ return originalFn ( url , options ) . then ( async ( ) => {
233+
234+ await processAccessibilityReport ( url ) ;
235+ } )
236+
237+ } ) ;
238+ } ) ;
239+ } else {
240+ Cypress . on ( 'command:start' , async ( command ) => {
241+ if ( ! command || ! command . attributes ) return ;
242+ if ( command . attributes . name == 'window' || command . attributes . name == 'then' || command . attributes . name == 'wrap' || command . attributes . name == 'wait' ) {
243+ return ;
244+ }
245+
246+ if ( ! commandsToWrap . includes ( command . attributes . name ) ) return ;
153247 let isAccessibilityLoaded = Cypress . env ( "ACCESSIBILITY" ) || false ;
154- if ( ! isAccessibilityLoaded ) {
155- console . log ( 'log' , "Accessibility not enabled." ) ;
156- return originalFn ( url , options ) ;
248+ if ( ! isAccessibilityLoaded ) {
249+ console . log ( 'log' , "accessibility not enabled " + isAccessibilityLoaded ) ;
250+ return ;
157251 }
158252
159253
160- return originalFn ( url , options ) . then ( async ( ) => {
161- await processAccessibilityReport ( url ) ;
254+ console . log ( 'log' , "debugging scan form command " + command . attributes . name ) ;
255+
256+ cy . window ( ) . then ( ( win ) => {
257+ oldprocessAccessibilityReport ( win ) ;
162258 } )
259+ } )
260+
261+ }
262+
163263
164- } ) ;
165- } ) ;
166264
167265
0 commit comments