@@ -10,10 +10,16 @@ import sharp from 'sharp';
10
10
async function captureScreenshotsForConfig (
11
11
ctx : Context ,
12
12
browsers : Record < string , Browser > ,
13
- { name , url , waitForTimeout } : Record < string , any > ,
13
+ urlConfig : Record < string , any > ,
14
14
browserName : string ,
15
15
renderViewports : Array < Record < string , any > >
16
16
) : Promise < void > {
17
+ ctx . log . debug ( `*** urlConfig ${ JSON . stringify ( urlConfig ) } ` ) ;
18
+
19
+ let { name, url, waitForTimeout, execute} = urlConfig ;
20
+ let afterNavigationScript = execute ?. afterNavigation ;
21
+ let beforeSnapshotScript = execute ?. beforeSnapshot ;
22
+
17
23
let pageOptions = { waitUntil : process . env . SMARTUI_PAGE_WAIT_UNTIL_EVENT || 'load' , timeout : ctx . config . waitForPageRender || constants . DEFAULT_PAGE_LOAD_TIMEOUT } ;
18
24
let ssId = name . toLowerCase ( ) . replace ( / \s / g, '_' ) ;
19
25
let context : BrowserContext ;
@@ -30,11 +36,15 @@ async function captureScreenshotsForConfig(
30
36
page = await context ?. newPage ( ) ;
31
37
32
38
await page ?. goto ( url . trim ( ) , pageOptions ) ;
39
+ await executeDocumentScripts ( ctx , page , "afterNavigation" , afterNavigationScript )
40
+
33
41
for ( let { viewport, viewportString, fullPage } of renderViewports ) {
34
42
let ssPath = `screenshots/${ ssId } /${ `${ browserName } -${ viewport . width } x${ viewport . height } ` } -${ ssId } .png` ;
35
43
await page ?. setViewportSize ( { width : viewport . width , height : viewport . height || constants . MIN_VIEWPORT_HEIGHT } ) ;
36
44
if ( fullPage ) await page ?. evaluate ( utils . scrollToBottomAndBackToTop ) ;
37
45
await page ?. waitForTimeout ( waitForTimeout || 0 ) ;
46
+ await executeDocumentScripts ( ctx , page , "beforeSnapshot" , beforeSnapshotScript )
47
+
38
48
await page ?. screenshot ( { path : ssPath , fullPage } ) ;
39
49
40
50
await ctx . client . uploadScreenshot ( ctx . build , ssPath , name , browserName , viewportString , ctx . log ) ;
@@ -126,7 +136,7 @@ export async function captureScreenshots(ctx: Context): Promise<Record<string,a
126
136
ctx . task . output = output ;
127
137
capturedScreenshots ++ ;
128
138
} catch ( error ) {
129
- ctx . log . debug ( `screenshot capture failed for ${ JSON . stringify ( staticConfig ) } ; error: ${ error } ` ) ;
139
+ ctx . log . debug ( `captureScreenshots failed for ${ JSON . stringify ( staticConfig ) } ; error: ${ error } ` ) ;
130
140
output += `${ chalk . gray ( staticConfig . name ) } ${ chalk . red ( '\u{2717}' ) } \n` ;
131
141
ctx . task . output = output ;
132
142
}
@@ -343,4 +353,21 @@ async function processChunk(ctx: Context, urlConfig: Array<Record<string, any>>)
343
353
344
354
await utils . closeBrowsers ( browsers ) ;
345
355
return { capturedScreenshots, finalOutput } ;
356
+ }
357
+
358
+ async function executeDocumentScripts ( ctx : Context , page : Page , actionType : string , script : string ) {
359
+ try {
360
+ if ( ! page ) {
361
+ throw new Error ( "Page instance not available" ) ;
362
+ }
363
+
364
+ if ( script !== "" ) {
365
+ await page . evaluate ( ( script ) => {
366
+ new Function ( script ) ( ) ;
367
+ } , script ) ;
368
+ }
369
+ } catch ( error ) {
370
+ ctx . log . error ( `Error executing script for action ${ actionType } : ` , error ) ;
371
+ throw error ;
372
+ }
346
373
}
0 commit comments