@@ -103,10 +103,6 @@ async function captureScreenshotsSync(
103103export async function captureScreenshots ( ctx : Context ) : Promise < Record < string , any > > {
104104 // Clean up directory to store screenshots
105105 utils . delDir ( 'screenshots' ) ;
106- console . log ( `ctx.options.parallel ${ ctx . options . parallel } ` ) ;
107-
108- // Check Parallel Option and divide web static config accordingly
109- console . log ( `Capturing screenshots for ${ ctx . webStaticConfig . length } URLs` ) ;
110106
111107 let browsers : Record < string , Browser > = { } ;
112108 let capturedScreenshots : number = 0 ;
@@ -254,3 +250,98 @@ export async function uploadScreenshots(ctx: Context): Promise<void> {
254250 ctx . log . info ( `${ noOfScreenshots } screenshots uploaded successfully.` ) ;
255251 }
256252}
253+
254+ export async function captureScreenshotsConcurrent ( ctx : Context ) : Promise < Record < string , any > > {
255+ // Clean up directory to store screenshots
256+ utils . delDir ( 'screenshots' ) ;
257+
258+ let totalSnapshots = ctx . webStaticConfig && ctx . webStaticConfig . length ;
259+ let browserInstances = ctx . options . parallel || 1 ;
260+ let optimizeBrowserInstances : number = 0
261+ optimizeBrowserInstances = Math . floor ( Math . log2 ( totalSnapshots ) ) ;
262+ if ( browserInstances != - 1 && optimizeBrowserInstances > browserInstances ) {
263+ optimizeBrowserInstances = browserInstances ;
264+ }
265+
266+ // If force flag is set, use the requested browser instances
267+ if ( ctx . options . force && browserInstances > 1 ) {
268+ optimizeBrowserInstances = browserInstances ;
269+ }
270+
271+ let urlsPerInstance : number = 0 ;
272+ if ( optimizeBrowserInstances == 1 ) {
273+ urlsPerInstance = totalSnapshots ;
274+ } else {
275+ urlsPerInstance = Math . ceil ( totalSnapshots / optimizeBrowserInstances ) ;
276+ }
277+ console . log ( `*** browserInstances requested ${ ctx . options . parallel } ` ) ;
278+ console . log ( `*** optimizeBrowserInstances ${ optimizeBrowserInstances } ` ) ;
279+ console . log ( `*** urlsPerInstance ${ urlsPerInstance } = ${ totalSnapshots } / ${ optimizeBrowserInstances } ` ) ;
280+ ctx . task . output = `Parallel Browser Instances: ${ optimizeBrowserInstances } \n` ;
281+ //Divide the URLs into chunks
282+ let staticURLChunks = splitURLs ( ctx . webStaticConfig , urlsPerInstance ) ;
283+ let totalCapturedScreenshots : number = 0 ;
284+ let output : any = '' ;
285+
286+ const responses = await Promise . all ( staticURLChunks . map ( async ( urlConfig ) => {
287+ console . log ( `@@@@ staticURLChunks ${ JSON . stringify ( urlConfig ) } ` ) ;
288+ let { capturedScreenshots, finalOutput} = await processChunk ( ctx , urlConfig ) ;
289+ return { capturedScreenshots, finalOutput } ;
290+ } ) ) ;
291+
292+ console . log ( `*** responses ${ JSON . stringify ( responses ) } ` ) ;
293+
294+ responses . forEach ( ( response : Record < string , any > ) => {
295+ totalCapturedScreenshots += response . capturedScreenshots ;
296+ output += response . finalOutput ;
297+ } ) ;
298+ console . log ( `*** totalCapturedScreenshots ${ totalCapturedScreenshots } ` ) ;
299+
300+ utils . delDir ( 'screenshots' ) ;
301+
302+ return { totalCapturedScreenshots, output } ;
303+ }
304+
305+ function splitURLs ( arr : any , chunkSize : number ) {
306+ const result = [ ] ;
307+ for ( let i = 0 ; i < arr . length ; i += chunkSize ) {
308+ result . push ( arr . slice ( i , i + chunkSize ) ) ;
309+ }
310+ return result ;
311+ }
312+
313+ async function processChunk ( ctx : Context , urlConfig : Array < Record < string , any > > ) : Promise < Record < string , any > > {
314+
315+ let browsers : Record < string , Browser > = { } ;
316+ let capturedScreenshots : number = 0 ;
317+ let finalOutput : string = '' ;
318+
319+ try {
320+ browsers = await utils . launchBrowsers ( ctx ) ;
321+ } catch ( error ) {
322+ await utils . closeBrowsers ( browsers ) ;
323+ ctx . log . debug ( error )
324+ throw new Error ( `Failed launching browsers ${ error } ` ) ;
325+ }
326+
327+ for ( let staticConfig of urlConfig ) {
328+ try {
329+ console . log ( `#### staticConfig ${ JSON . stringify ( staticConfig ) } ` ) ;
330+ await captureScreenshotsAsync ( ctx , staticConfig , browsers ) ;
331+
332+ utils . delDir ( `screenshots/${ staticConfig . name . toLowerCase ( ) . replace ( / \s / g, '_' ) } ` ) ;
333+ let output = ( `${ chalk . gray ( staticConfig . name ) } ${ chalk . green ( '\u{2713}' ) } \n` ) ;
334+ ctx . task . output = ctx . task . output ? ctx . task . output + output : output ;
335+ finalOutput += output ;
336+ capturedScreenshots ++ ;
337+ } catch ( error ) {
338+ ctx . log . debug ( `screenshot capture failed for ${ JSON . stringify ( staticConfig ) } ; error: ${ error } ` ) ;
339+ let output = `${ chalk . gray ( staticConfig . name ) } ${ chalk . red ( '\u{2717}' ) } \n` ;
340+ ctx . task . output += output ;
341+ finalOutput += output ;
342+ }
343+ }
344+
345+ await utils . closeBrowsers ( browsers ) ;
346+ return { capturedScreenshots, finalOutput } ;
347+ }
0 commit comments