@@ -691,8 +691,24 @@ class Playwright extends Helper {
691691 } else {
692692 this . activeSessionName = session
693693 }
694- const existingPages = await this . browserContext . pages ( )
695- await this . _setPage ( existingPages [ 0 ] )
694+
695+ // Safety check: ensure browserContext exists before calling pages()
696+ if ( ! this . browserContext ) {
697+ this . debug ( 'Cannot restore session vars: browserContext is undefined' )
698+ return
699+ }
700+
701+ try {
702+ const existingPages = await this . browserContext . pages ( )
703+ if ( existingPages && existingPages . length > 0 ) {
704+ await this . _setPage ( existingPages [ 0 ] )
705+ } else {
706+ this . debug ( 'Cannot restore session vars: no pages available' )
707+ }
708+ } catch ( err ) {
709+ this . debug ( `Failed to restore session vars: ${ err . message } ` )
710+ return
711+ }
696712
697713 return this . _waitForAction ( )
698714 } ,
@@ -980,6 +996,36 @@ class Playwright extends Helper {
980996 }
981997 }
982998
999+ // Ensure browser is initialized before page operations
1000+ if ( ! this . page ) {
1001+ this . debugSection ( 'Auto-initializing' , `Browser not started properly. page=${ ! ! this . page } , isRunning=${ this . isRunning } , browser=${ ! ! this . browser } , browserContext=${ ! ! this . browserContext } ` )
1002+
1003+ if ( ! this . browser ) {
1004+ await this . _startBrowser ( )
1005+ }
1006+
1007+ // Create browser context and page (simplified version of _before logic)
1008+ if ( ! this . browserContext ) {
1009+ const contextOptions = {
1010+ ignoreHTTPSErrors : this . options . ignoreHTTPSErrors ,
1011+ acceptDownloads : true ,
1012+ ...this . options . emulate ,
1013+ }
1014+ this . browserContext = await this . browser . newContext ( contextOptions )
1015+ }
1016+
1017+ const pages = await this . browserContext . pages ( )
1018+ const mainPage = pages [ 0 ] || ( await this . browserContext . newPage ( ) )
1019+ await this . _setPage ( mainPage )
1020+
1021+ this . debugSection ( 'Auto-initializing' , `Completed. page=${ ! ! this . page } , browserContext=${ ! ! this . browserContext } ` )
1022+ }
1023+
1024+ // Additional safety check
1025+ if ( ! this . page ) {
1026+ throw new Error ( `Page is not initialized after auto-initialization. this.page=${ this . page } , this.isRunning=${ this . isRunning } , this.browser=${ ! ! this . browser } , this.browserContext=${ ! ! this . browserContext } ` )
1027+ }
1028+
9831029 await this . page . goto ( url , { waitUntil : this . options . waitForNavigation } )
9841030
9851031 const performanceTiming = JSON . parse ( await this . page . evaluate ( ( ) => JSON . stringify ( window . performance . timing ) ) )
@@ -2305,11 +2351,16 @@ class Playwright extends Helper {
23052351 async saveElementScreenshot ( locator , fileName ) {
23062352 const outputFile = screenshotOutputFolder ( fileName )
23072353
2308- const res = await this . _locateElement ( locator )
2309- assertElementExists ( res , locator )
2310- const elem = res
2311- this . debug ( `Screenshot of ${ new Locator ( locator ) } element has been saved to ${ outputFile } ` )
2312- return elem . screenshot ( { path : outputFile , type : 'png' } )
2354+ try {
2355+ const res = await this . _locateElement ( locator )
2356+ assertElementExists ( res , locator )
2357+ const elem = res
2358+ this . debug ( `Screenshot of ${ new Locator ( locator ) } element has been saved to ${ outputFile } ` )
2359+ return elem . screenshot ( { path : outputFile , type : 'png' } )
2360+ } catch ( err ) {
2361+ this . debug ( `Failed to take element screenshot: ${ err . message } ` )
2362+ throw err
2363+ }
23132364 }
23142365
23152366 /**
@@ -2321,25 +2372,43 @@ class Playwright extends Helper {
23212372
23222373 this . debugSection ( 'Screenshot' , relativeDir ( outputFile ) )
23232374
2324- await this . page . screenshot ( {
2325- path : outputFile ,
2326- fullPage : fullPageOption ,
2327- type : 'png' ,
2328- } )
2375+ if ( ! this . page ) {
2376+ this . debug ( 'Cannot take screenshot: page object is undefined' )
2377+ return
2378+ }
23292379
2330- if ( this . activeSessionName ) {
2380+ try {
2381+ await this . page . screenshot ( {
2382+ path : outputFile ,
2383+ fullPage : fullPageOption ,
2384+ type : 'png' ,
2385+ } )
2386+ } catch ( err ) {
2387+ this . debug ( `Failed to take screenshot: ${ err . message } ` )
2388+ throw err
2389+ }
2390+
2391+ // Handle session screenshots for ALL sessions, not just active one
2392+ if ( this . sessionPages && Object . keys ( this . sessionPages ) . length > 0 ) {
23312393 for ( const sessionName in this . sessionPages ) {
2332- const activeSessionPage = this . sessionPages [ sessionName ]
2394+ const sessionPage = this . sessionPages [ sessionName ]
23332395 outputFile = screenshotOutputFolder ( `${ sessionName } _${ fileName } ` )
23342396
23352397 this . debugSection ( 'Screenshot' , `${ sessionName } - ${ relativeDir ( outputFile ) } ` )
23362398
2337- if ( activeSessionPage ) {
2338- await activeSessionPage . screenshot ( {
2339- path : outputFile ,
2340- fullPage : fullPageOption ,
2341- type : 'png' ,
2342- } )
2399+ try {
2400+ if ( sessionPage && ! sessionPage . isClosed ( ) ) {
2401+ await sessionPage . screenshot ( {
2402+ path : outputFile ,
2403+ fullPage : fullPageOption ,
2404+ type : 'png' ,
2405+ } )
2406+ } else {
2407+ this . debug ( `Cannot take session screenshot: session page for '${ sessionName } ' is closed or undefined` )
2408+ }
2409+ } catch ( err ) {
2410+ this . debug ( `Failed to take session screenshot for '${ sessionName } ': ${ err . message } ` )
2411+ // Don't throw here - main screenshot was successful
23432412 }
23442413 }
23452414 }
0 commit comments