@@ -243,6 +243,47 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
243243 ctx . log . debug ( 'No valid cookies to add' ) ;
244244 }
245245 }
246+
247+ let options = snapshot . options ;
248+
249+ // Custom cookies include those which cannot be captured by javascript function `document.cookie` like httpOnly, secure, sameSite etc.
250+ // These custom cookies will be captured by the user in their automation browser and sent to CLI through the snapshot options using `customCookies` field.
251+ if ( options ?. customCookies && Array . isArray ( options . customCookies ) && options . customCookies . length > 0 ) {
252+ ctx . log . debug ( `Setting ${ options . customCookies . length } custom cookies` ) ;
253+
254+ const validCustomCookies = options . customCookies . filter ( cookie => {
255+ if ( ! cookie . name || ! cookie . value || ! cookie . domain ) {
256+ ctx . log . debug ( `Skipping invalid custom cookie: missing required fields (name, value, or domain)` ) ;
257+ return false ;
258+ }
259+
260+ if ( cookie . sameSite && ! [ 'Strict' , 'Lax' , 'None' ] . includes ( cookie . sameSite ) ) {
261+ ctx . log . debug ( `Skipping invalid custom cookie: invalid sameSite value '${ cookie . sameSite } '` ) ;
262+ return false ;
263+ }
264+
265+ return true ;
266+ } ) . map ( cookie => ( {
267+ name : cookie . name ,
268+ value : cookie . value ,
269+ domain : cookie . domain ,
270+ path : cookie . path || '/' ,
271+ httpOnly : cookie . httpOnly || false ,
272+ secure : cookie . secure || false ,
273+ sameSite : cookie . sameSite || 'Lax'
274+ } ) ) ;
275+
276+ if ( validCustomCookies . length > 0 ) {
277+ try {
278+ await context . addCookies ( validCustomCookies ) ;
279+ ctx . log . debug ( `Successfully added ${ validCustomCookies . length } custom cookies` ) ;
280+ } catch ( error ) {
281+ ctx . log . debug ( `Failed to add custom cookies: ${ error } ` ) ;
282+ }
283+ } else {
284+ ctx . log . debug ( 'No valid custom cookies to add' ) ;
285+ }
286+ }
246287 const page = await context . newPage ( ) ;
247288
248289 // populate cache with already captured resources
@@ -415,8 +456,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
415456 route . abort ( ) ;
416457 }
417458 } ) ;
418-
419- let options = snapshot . options ;
420459 let optionWarnings : Set < string > = new Set ( ) ;
421460 let selectors : Array < string > = [ ] ;
422461 let ignoreOrSelectDOM : string ;
@@ -582,6 +621,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
582621 // adding extra timeout since domcontentloaded event is fired pretty quickly
583622 await new Promise ( r => setTimeout ( r , 1250 ) ) ;
584623 if ( ctx . config . waitForTimeout ) await page . waitForTimeout ( ctx . config . waitForTimeout ) ;
624+ await page . waitForLoadState ( "networkidle" , { timeout : 10000 } ) . catch ( ( ) => { ctx . log . debug ( 'networkidle event failed to fire within 10s' ) } ) ;
585625 navigated = true ;
586626 ctx . log . debug ( `Navigated to ${ snapshot . url } ` ) ;
587627 } catch ( error : any ) {
@@ -815,7 +855,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
815855
816856 if ( hasBrowserErrors ) {
817857 discoveryErrors . timestamp = new Date ( ) . toISOString ( ) ;
818- // ctx.log.warn(discoveryErrors);
819858 }
820859
821860 if ( ctx . config . useGlobalCache ) {
0 commit comments