import { logger } from 'flex-dev-utils' ; import { launch , Browser as PuppeteerBrowser , Page , ConsoleMessage , ConsoleMessageType } from 'puppeteer' ; import assertion from './assertion' ; import { App , BaseUrl } from './pages' ; export class Browser { static app : App ; private static _browser : PuppeteerBrowser ; private static _page : Page ; private static _domainsToInclude = [ 'twilio' , 'localhost' , 'unpkg' ] ; /**
     * Initializes browser object
     */ static async create ( baseUrls ) { this . _browser = await launch ( { headless : true , args : [ '--use-fake-ui-for-media-stream' ] } ) ; this . _page = await this . _browser . newPage ( ) ; await this . _page . setRequestInterception ( true ) ; this . _attachLogListener ( ) ; this . _attachNetworkInterceptor ( ) ; this . app = new App ( this . _page , baseUrls ) ; assertion . app . init ( this . app ) ; } static async kill ( ) { try { this . _page . removeAllListeners ( ) ; await this . _browser . close ( ) ; } catch ( e ) { logger . error ( 'Failed to quit browser' ) ; } } /**
     * Attach browser log listener
     */ private static _attachNetworkInterceptor ( ) { this . _page . on ( 'request' , async ( request ) : Promise < void > => { const url = request . url ( ) ; const re = new RegExp ( this . _domainsToInclude . join ( '|' ) ) ; if ( request . resourceType ( ) === 'script' && ! re . test ( url ) ) { return request . abort ( ) ; } return request . continue ( ) ; } ) ; } /**
     * Attach network interceptor
     */ private static _attachLogListener ( ) { this . _page . on ( 'console' , ( msg ) => { const logTypes = [ 'error' ] ; const url = msg . location ( ) . url || '' ; const re = new RegExp ( this . _domainsToInclude . join ( '|' ) ) ; if ( logTypes . includes ( msg . type ( ) ) && re . test ( url ) ) { logger . error ( { msg : msg . text ( ) , url , args : msg . args ( ) || '' , lineNumber : msg . location ( ) . lineNumber , colNumber : msg . location ( ) . columnNumber , stackTrace : msg . stackTrace ( ) || '' , } ) ; } } ) ; } }