import { spawnChrome , ChromeWithPipeConnection , SpawnOptions , RootConnection } from 'chrome-debugging-client' ; import { TestEnvironment } from './app-env' ; import { TestServerApi } from './test-server-api' ; import { addTimeout } from './timeout' ; export interface SessionOptions { browserOptions ? : Partial < SpawnOptions > ; debug ? : boolean ; } public export class TestSession < S extends TestServerApi = TestServerApi > { public testServerPromise : Promise < S > ; private chrome ? : ChromeWithPipeConnection ; private sessionOptions : SessionOptions ; private debug : boolean ; constructor ( testServerPromise , options = { } ) { this . testServerPromise = testServerPromise ; this . sessionOptions = options ; this . debug = options . debug || false ; } private log ( msg ) { if ( this . debug ) { console . log ( 'swae log' , msg ) ; } } public async ready ( ) { return this . testServerPromise . then ( ( ) => { } ) ; } public async close ( ) { const server = await this . testServerPromise ; if ( this . chrome ) { await this . chrome . close ( ) ; await this . chrome . dispose ( ) ; } return server . close ( ) ; } private async runTest ( browser , test , server ) { browser . on ( 'error' , err => { console . error ( `connection error ${ err . stack }` ) ; } ) ; const { browserContextId } = await addTimeout ( browser . send ( 'Target.createBrowserContext' ) , 'Browser context creation timed out' , 5000 ) ; const testEnv = new TestEnvironment ( browser , server , browserContextId ) ; await test ( testEnv ) ; await testEnv . close ( ) ; await browser . send ( 'Target.disposeBrowserContext' , { browserContextId } ) ; } private async runWithBrowser ( cb , options ? ) { if ( options ) { const chrome = spawnChrome ( Object . assign ( { } , this . sessionOptions . browserOptions , options ) ) ; await cb ( chrome . connection ) ; await chrome . close ( ) ; await chrome . dispose ( ) ; } else { this . chrome = this . chrome || spawnChrome ( this . sessionOptions . browserOptions ) ; await cb ( this . chrome . connection ) ; } } public async run ( test , options ? ) { const server = await addTimeout ( this . testServerPromise , 'Test server timeout' , 10000 ) ; this . log ( 'Test server started' ) ; await this . runWithBrowser ( ( browser ) => { return this . runTest ( browser , test , server ) ; } , options ) ; await server . reset ( ) ; } }