1
+ const fs = require ( "fs" )
2
+
3
+ const LambdatestLog = ( message ) => {
4
+ if ( ! Cypress . env ( 'LAMBDATEST_LOGS' ) ) return ;
5
+ cy . task ( 'lambdatest_log' , message ) ;
6
+ }
7
+
8
+ const commandsToWrap = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scroll' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
9
+
10
+ const setScanConfig = ( win , payload ) =>
11
+ new Promise ( async ( resolve , reject ) => {
12
+ const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
13
+ if ( ! isHttpOrHttps ) {
14
+ resolve ( ) ;
15
+ }
16
+
17
+ function startScan ( ) {
18
+ console . log ( 'log' , "Accessibility setting scan config" )
19
+ function onScanComplete ( event ) {
20
+ win . document . removeEventListener ( "automation-custom-event" , onScanComplete ) ;
21
+ console . log ( 'log' , "Recieved scan config data " + event . detail )
22
+ resolve ( event . detail ) ;
23
+ }
24
+
25
+ win . document . addEventListener ( "automation-custom-event" , onScanComplete ) ;
26
+ const e = new CustomEvent ( "accessibility-extension-custom-event" , { detail : payload } ) ;
27
+ win . document . dispatchEvent ( e ) ;
28
+
29
+
30
+ setTimeout ( ( ) => {
31
+ reject ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
32
+ } , 9000 ) ;
33
+ }
34
+ startScan ( ) ;
35
+
36
+ } )
37
+
38
+ const getScanData = ( win , payload ) =>
39
+ new Promise ( async ( resolve , reject ) => {
40
+ const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
41
+ if ( ! isHttpOrHttps ) {
42
+ resolve ( ) ;
43
+ }
44
+
45
+
46
+ function getSummary ( ) {
47
+ function onReceiveSummary ( event ) {
48
+
49
+ win . document . removeEventListener ( "automation-custom-event" , onReceiveSummary ) ;
50
+ resolve ( event . detail ) ;
51
+ }
52
+
53
+
54
+ win . document . addEventListener ( "automation-custom-event" , onReceiveSummary ) ;
55
+ const e = new CustomEvent ( "accessibility-extension-custom-event" , { detail : payload } ) ;
56
+ win . document . dispatchEvent ( e ) ;
57
+
58
+ setTimeout ( ( ) => {
59
+ reject ( new Error ( 'automation-custom-event not received within timeout' ) ) ;
60
+ } , 9000 ) ;
61
+
62
+ }
63
+
64
+
65
+ getSummary ( ) ;
66
+
67
+ } )
68
+
69
+ const shouldScanForAccessibility = ( attributes ) => {
70
+ if ( Cypress . env ( "IS_ACCESSIBILITY_EXTENSION_LOADED" ) !== "true" ) return false ;
71
+
72
+ const extensionPath = Cypress . env ( "ACCESSIBILITY_EXTENSION_PATH" ) ;
73
+ const isHeaded = Cypress . browser . isHeaded ;
74
+
75
+ if ( ! isHeaded || ( extensionPath === undefined ) ) return false ;
76
+
77
+ let shouldScanTestForAccessibility = true ;
78
+
79
+ if ( Cypress . env ( "INCLUDE_TAGS_FOR_ACCESSIBILITY" ) || Cypress . env ( "EXCLUDE_TAGS_FOR_ACCESSIBILITY" ) ) {
80
+ try {
81
+ let includeTagArray = [ ] ;
82
+ let excludeTagArray = [ ] ;
83
+ if ( Cypress . env ( "INCLUDE_TAGS_FOR_ACCESSIBILITY" ) ) {
84
+ includeTagArray = Cypress . env ( "INCLUDE_TAGS_FOR_ACCESSIBILITY" ) . split ( ";" )
85
+ }
86
+ if ( Cypress . env ( "EXCLUDE_TAGS_FOR_ACCESSIBILITY" ) ) {
87
+ excludeTagArray = Cypress . env ( "EXCLUDE_TAGS_FOR_ACCESSIBILITY" ) . split ( ";" )
88
+ }
89
+
90
+ const fullTestName = attributes . title ;
91
+ const excluded = excludeTagArray . some ( ( exclude ) => fullTestName . includes ( exclude ) ) ;
92
+ const included = includeTagArray . length === 0 || includeTags . some ( ( include ) => fullTestName . includes ( include ) ) ;
93
+ shouldScanTestForAccessibility = ! excluded && included ;
94
+ } catch ( error ) {
95
+ LambdatestLog ( "Error while validating test case for accessibility before scanning. Error : " , error ) ;
96
+ }
97
+ }
98
+
99
+ return shouldScanTestForAccessibility ;
100
+ }
101
+
102
+ Cypress . on ( 'command:start' , async ( command ) => {
103
+ if ( ! command || ! command . attributes ) return ;
104
+ if ( command . attributes . name == 'window' || command . attributes . name == 'then' || command . attributes . name == 'wrap' || command . attributes . name == 'wait' ) {
105
+ return ;
106
+ }
107
+
108
+ if ( ! commandsToWrap . includes ( command . attributes . name ) ) return ;
109
+
110
+ // const attributes = Cypress.mocha.getRunner().suite.ctx.currentTest || Cypress.mocha.getRunner().suite.ctx._runnable;
111
+
112
+ // let shouldScanTestForAccessibility = shouldScanForAccessibility(attributes);
113
+ // if (!shouldScanTestForAccessibility) return;
114
+ // console.log('log', "debugging scan form command " + command.attributes.name);
115
+ console . log ( 'log' , "debugging scan form command " + command . attributes . name ) ;
116
+ cy . window ( ) . then ( ( win ) => {
117
+ // LambdatestLog('Performing scan form command ' + command.attributes.name);
118
+ let wcagCriteriaValue = Cypress . env ( "WCAG_CRITERIA" ) || "wcag21a" ;
119
+ let bestPracticeValue = Cypress . env ( "BEST_PRACTICE" ) || false ;
120
+ let needsReviewValue = Cypress . env ( "NEEDS_REVIEW" ) || true ;
121
+
122
+ const payloadToSend = {
123
+ message : 'SET_CONFIG' ,
124
+ wcagCriteria : wcagCriteriaValue ,
125
+ bestPractice : bestPracticeValue ,
126
+ needsReview : needsReviewValue
127
+ }
128
+ let testId = Cypress . env ( "TEST_ID" ) || ""
129
+ // const filePath = 'cypress/reports/accessibilityReport_' + testId + '.json';
130
+ const filePath = Cypress . env ( "ACCESSIBILITY_REPORT_PATH" ) || 'cypress/results/accessibilityReport_' + testId + '.json' ;
131
+
132
+ cy . wrap ( setScanConfig ( win , payloadToSend ) , { timeout : 30000 } ) . then ( ( res ) => {
133
+ // LambdatestLog('log', "logging report **************")
134
+ console . log ( 'logging config reponse' , res ) ;
135
+
136
+ const payload = {
137
+ message : 'GET_LATEST_SCAN_DATA' ,
138
+ }
139
+ // cy.wait(5000);
140
+ cy . wrap ( getScanData ( win , payload ) , { timeout : 30000 } ) . then ( ( res ) => {
141
+ LambdatestLog ( 'log' , "logging report **************" )
142
+
143
+
144
+ cy . task ( 'initializeFile' , filePath ) . then ( ( filePath ) => {
145
+ cy . readFile ( filePath , { log : true , timeout : 30000 } ) . then ( ( fileContent ) => {
146
+ let resultsArray = [ { } ] ;
147
+ console . log ( 'logging report' , res ) ;
148
+ // If the file is not empty, parse the existing content
149
+ if ( fileContent ) {
150
+ try {
151
+ resultsArray = JSON . parse ( JSON . stringify ( fileContent ) ) ;
152
+ } catch ( e ) {
153
+ console . log ( "parsing error for content " , fileContent )
154
+ console . log ( 'Error parsing JSON file:' , e ) ;
155
+ return ;
156
+ }
157
+ }
158
+ console . log ( 'debugging res' , res . message ) ;
159
+ if ( res . message == "GET_LATEST_SCAN_DATA" ) {
160
+ // Append the new result
161
+ resultsArray . push ( res ) ;
162
+ console . log ( 'resultsarray logging' , resultsArray ) ;
163
+ }
164
+
165
+ // Write the updated content back to the file
166
+ cy . writeFile ( filePath , resultsArray , { log : true , timeout : 30000 } ) ;
167
+ } ) ;
168
+ } ) ;
169
+ } ) ;
170
+
171
+ } ) ;
172
+ } )
173
+ } )
174
+
175
+
176
+ Cypress . on ( 'command:end' , ( command ) => {
177
+
178
+ return ;
179
+ } )
0 commit comments