@@ -58,7 +58,8 @@ export class ElectronApplication extends SdkObject {
5858 private _nodeConnection : CRConnection ;
5959 private _nodeSession : CRSession ;
6060 private _nodeExecutionContext : js . ExecutionContext | undefined ;
61- _nodeElectronHandle : js . JSHandle < any > | undefined ;
61+ _nodeElectronHandlePromised : Promise < js . JSHandle < any > > ;
62+ private _resolveNodeElectronHandle ! : ( handle : js . JSHandle < any > ) => void ;
6263 private _windows = new Set < ElectronPage > ( ) ;
6364 private _lastWindowId = 0 ;
6465 readonly _timeoutSettings = new TimeoutSettings ( ) ;
@@ -73,6 +74,7 @@ export class ElectronApplication extends SdkObject {
7374 this . _browserContext . on ( BrowserContext . Events . Page , event => this . _onPage ( event ) ) ;
7475 this . _nodeConnection = nodeConnection ;
7576 this . _nodeSession = nodeConnection . rootSession ;
77+ this . _nodeElectronHandlePromised = new Promise ( resolve => this . _resolveNodeElectronHandle = resolve ) ;
7678 }
7779
7880 private async _onPage ( page : ElectronPage ) {
@@ -87,7 +89,7 @@ export class ElectronApplication extends SdkObject {
8789 this . _windows . add ( page ) ;
8890
8991 // Below is async.
90- const handle = await this . _nodeElectronHandle ! . evaluateHandle ( ( { BrowserWindow } , windowId ) => BrowserWindow . fromId ( windowId ) , windowId ) . catch ( e => { } ) ;
92+ const handle = await ( await this . _nodeElectronHandlePromised ) . evaluateHandle ( ( { BrowserWindow } , windowId ) => BrowserWindow . fromId ( windowId ) , windowId ) . catch ( e => { } ) ;
9193 if ( ! handle )
9294 return ;
9395 page . browserWindow = handle ;
@@ -103,7 +105,7 @@ export class ElectronApplication extends SdkObject {
103105 async close ( ) {
104106 const progressController = new ProgressController ( internalCallMetadata ( ) , this ) ;
105107 const closed = progressController . run ( progress => helper . waitForEvent ( progress , this , ElectronApplication . Events . Close ) . promise , this . _timeoutSettings . timeout ( { } ) ) ;
106- await this . _nodeElectronHandle ! . evaluate ( ( { app } ) => app . quit ( ) ) ;
108+ await ( await this . _nodeElectronHandlePromised ) . evaluate ( ( { app } ) => app . quit ( ) ) ;
107109 this . _nodeConnection . close ( ) ;
108110 await closed ;
109111 }
@@ -114,7 +116,8 @@ export class ElectronApplication extends SdkObject {
114116 this . _nodeExecutionContext = new js . ExecutionContext ( this , new CRExecutionContext ( this . _nodeSession , event . context ) ) ;
115117 } ) ;
116118 await this . _nodeSession . send ( 'Runtime.enable' , { } ) . catch ( e => { } ) ;
117- this . _nodeElectronHandle = await js . evaluate ( this . _nodeExecutionContext ! , false /* returnByValue */ , `process.mainModule.require('electron')` ) ;
119+ js . evaluate ( this . _nodeExecutionContext ! , false /* returnByValue */ , `process.mainModule.require('electron')` )
120+ . then ( this . _resolveNodeElectronHandle ) ;
118121 }
119122}
120123
0 commit comments