@@ -4,7 +4,8 @@ const fs = require('fs'),
44 logger = require ( './logger' ) . winstonLogger ,
55 utils = require ( "./utils" ) ,
66 Constants = require ( './constants' ) ,
7- config = require ( "./config" ) ;
7+ config = require ( "./config" ) ,
8+ axios = require ( "axios" ) ;
89
910let templatesDir = path . join ( __dirname , '../' , 'templates' ) ;
1011
@@ -100,7 +101,7 @@ let reportGenerator = (bsConfig, buildId, args, cb) => {
100101 } ,
101102 } ;
102103
103- return request . get ( options , function ( err , resp , body ) {
104+ return request . get ( options , async function ( err , resp , body ) {
104105 let message = null ;
105106 let messageType = null ;
106107 let errorCode = null ;
@@ -163,7 +164,7 @@ let reportGenerator = (bsConfig, buildId, args, cb) => {
163164 } else {
164165 messageType = Constants . messageTypes . SUCCESS ;
165166 message = `Report for build: ${ buildId } was successfully created.` ;
166- renderReportHTML ( build ) ;
167+ await renderReportHTML ( build ) ;
167168 logger . info ( message ) ;
168169 }
169170 utils . sendUsageReport ( bsConfig , args , message , messageType , errorCode ) ;
@@ -173,7 +174,7 @@ let reportGenerator = (bsConfig, buildId, args, cb) => {
173174 } ) ;
174175}
175176
176- function renderReportHTML ( report_data ) {
177+ async function renderReportHTML ( report_data ) {
177178 let resultsDir = 'results' ;
178179 let metaCharSet = `<meta charset="utf-8">` ;
179180 let metaViewPort = `<meta name="viewport" content="width=device-width, initial-scale=1"> ` ;
@@ -194,12 +195,17 @@ function renderReportHTML(report_data) {
194195 }
195196
196197 // Writing the JSON used in creating the HTML file.
197- fs . writeFileSync ( `${ resultsDir } /browserstack-cypress-report.json` , JSON . stringify ( report_data ) , ( ) => {
198- if ( err ) {
199- return logger . error ( err ) ;
198+ let reportData = await cypressReportData ( report_data ) ;
199+ fs . writeFileSync (
200+ `${ resultsDir } /browserstack-cypress-report.json` ,
201+ JSON . stringify ( reportData ) ,
202+ ( ) => {
203+ if ( err ) {
204+ return logger . error ( err ) ;
205+ }
206+ logger . info ( "The JSON file is saved" ) ;
200207 }
201- logger . info ( "The JSON file is saved" ) ;
202- } ) ;
208+ ) ;
203209
204210 // Writing the HTML file generated from the JSON data.
205211 fs . writeFileSync ( `${ resultsDir } /browserstack-cypress-report.html` , html , ( ) => {
@@ -210,4 +216,100 @@ function renderReportHTML(report_data) {
210216 } ) ;
211217}
212218
219+ async function cypressReportData ( report_data ) {
220+ specFiles = Object . keys ( report_data . rows ) ;
221+ combinationPromises = [ ] ;
222+ for ( let spec of specFiles ) {
223+ let specSessions = report_data . rows [ spec ] [ "sessions" ] ;
224+ if ( specSessions . length > 0 ) {
225+ for ( let combination of specSessions ) {
226+ if ( utils . isUndefined ( report_data . cypress_version ) || report_data . cypress_version < "6" ) {
227+ combinationPromises . push ( generateCypressCombinationSpecReportDataWithoutConfigJson ( combination ) ) ;
228+ } else {
229+ combinationPromises . push ( generateCypressCombinationSpecReportDataWithConfigJson ( combination ) ) ;
230+ }
231+ }
232+ }
233+ }
234+ await Promise . all ( combinationPromises ) ;
235+ return report_data ;
236+ }
237+
238+ function generateCypressCombinationSpecReportDataWithConfigJson ( combination ) {
239+ return new Promise ( async ( resolve , reject ) => {
240+ try {
241+ let configJsonError , resultsJsonError ;
242+ let [ configJsonResponse , resultsJsonResponse ] = await axios . all ( [
243+ axios . get ( combination . tests . config_json ) . catch ( function ( error ) {
244+ configJsonError = true ;
245+ } ) ,
246+ axios . get ( combination . tests . result_json ) . catch ( function ( error ) {
247+ resultsJsonError = true ;
248+ } )
249+ ] ) ;
250+ if ( resultsJsonError || configJsonError ) {
251+ resolve ( ) ;
252+ }
253+ let tests = { } ;
254+ let configJson = configJsonResponse . data ;
255+ let resultsJson = resultsJsonResponse . data ;
256+ if ( utils . isUndefined ( configJson . tests ) || utils . isUndefined ( resultsJson . tests ) ) {
257+ resolve ( ) ;
258+ }
259+ configJson . tests . forEach ( ( test ) => {
260+ tests [ test [ "clientId" ] ] = test ;
261+ } ) ;
262+ resultsJson . tests . forEach ( ( test ) => {
263+ tests [ test [ "clientId" ] ] = Object . assign (
264+ tests [ test [ "clientId" ] ] ,
265+ test
266+ ) ;
267+ } ) ;
268+ let sessionTests = [ ] ;
269+ Object . keys ( tests ) . forEach ( ( testId ) => {
270+ sessionTests . push ( {
271+ name : tests [ testId ] [ "title" ] . pop ( ) ,
272+ status : tests [ testId ] [ "state" ] ,
273+ duration : parseFloat (
274+ tests [ testId ] [ "attempts" ] . pop ( ) [ "wallClockDuration" ] / 1000
275+ ) . toFixed ( 2 ) ,
276+ } ) ;
277+ } ) ;
278+ combination . tests = sessionTests ;
279+ resolve ( combination . tests ) ;
280+ } catch ( error ) { reject ( error ) }
281+ } )
282+ }
283+
284+ function generateCypressCombinationSpecReportDataWithoutConfigJson ( combination ) {
285+ return new Promise ( async ( resolve , reject ) => {
286+ try {
287+ let resultsJsonError ;
288+ let resultsJsonResponse = await axios . get ( combination . tests . result_json ) . catch ( function ( error ) {
289+ resultsJsonError = true ;
290+ } ) ;
291+ if ( resultsJsonError || utils . isUndefined ( resultsJsonResponse ) ) {
292+ resolve ( ) ;
293+ }
294+ let resultsJson = resultsJsonResponse . data ;
295+ let sessionTests = [ ] ;
296+ if ( utils . isUndefined ( resultsJson . tests ) ) {
297+ resolve ( ) ;
298+ }
299+ resultsJson . tests . forEach ( ( test ) => {
300+ durationKey = utils . isUndefined ( test [ "attempts" ] ) ? test : test [ "attempts" ] . pop ( )
301+ sessionTests . push ( {
302+ name : test [ "title" ] . pop ( ) ,
303+ status : test [ "state" ] ,
304+ duration : parseFloat (
305+ durationKey [ "wallClockDuration" ] / 1000
306+ ) . toFixed ( 2 )
307+ } )
308+ } ) ;
309+ combination . tests = sessionTests ;
310+ resolve ( combination . tests ) ;
311+ } catch ( error ) { reject ( error ) }
312+ } )
313+ }
314+
213315exports . reportGenerator = reportGenerator ;
0 commit comments