import { sleep } from '../../../utils' ; import { Browser } from 'playwright' ; export interface CloseOptions { type { number } memberof timeoutInMilliseconds : number ; } export const defaultCloseOptions = { timeoutInMilliseconds : 3000 , } ; export async function closeBrowser ( browser , options ) { if ( browser === undefined ) { return ; } const contexts = browser . contexts ( ) ; if ( Array . isArray ( contexts ) && contexts . length > 0 ) { for ( let index = 0 ; index < contexts . length ; index ++ ) { const context = contexts [ index ] ; try { await Promise . race ( [ context . close ( ) , sleep ( options . timeoutInMilliseconds ) ] ) ; } catch ( error ) { console . warn ( 'Error while closing the browser context' , error ) ; } } } try { await Promise . race ( [ browser . close ( ) , sleep ( options . timeoutInMilliseconds ) ] ) ; } catch ( error ) { console . warn ( 'Error while closing the browser' , error ) ; } }