export async function browserExecute ( fn ) { return browser . execute ( fn ) ; } export async function browserExecuteAsync < R , A , B > ( fn , a , b ) ; export async function browserExecuteAsync < R , A > ( fn , arg ) ; export async function browserExecuteAsync < R > ( fn ) ; export async function browserExecuteAsync < A extends any [ ] > ( fn , ... args ) { return browser . executeAsync ( fn as any , ... args ) ; } interface BrowserLog { level : string ; message : string ; source : string ; timestamp : number ; } export async function withBrowserLogs ( fn ) { if ( browser . getLogs ) { const logs = ( await browser . getLogs ( 'browser' ) ) as BrowserLog [ ] ; fn ( logs ) ; } } export async function flushBrowserLogs ( ) { await withBrowserLogs ( ( ) => { } ) ; } export async function deleteAllCookies ( ) { return browserExecute ( ( ) => { const cookies = document . cookie . split ( ';' ) ; for ( const cookie of cookies ) { const eqPos = cookie . indexOf ( '=' ) ; const name = eqPos > - 1 ? cookie . substr ( 0 , eqPos ) : cookie ; document . cookie = `${ name }=;expires=Thu, 01 Jan 1970 00:00:00 GMT;samesite=strict` ; } } ) ; } export async function sendXhr ( url , headers = [ ] ) { type State = { state : 'success' ; response : string ; } | { state : 'error' ; } ; const result = await browserExecuteAsync ( ( url , headers , done ) => { const xhr = new XMLHttpRequest ( ) ; let state = { state : 'error' } ; xhr . addEventListener ( 'load' , ( ) => { state = { state : 'success' , response : xhr . response as string } ; } ) ; xhr . addEventListener ( 'loadend' , ( ) => done ( state ) ) ; xhr . open ( 'GET' , url ) ; headers . forEach ( ( header ) => xhr . setRequestHeader ( header [ 0 ] , header [ 1 ] ) ) ; xhr . send ( ) ; } , url , headers ) ; if ( result . state === 'error' ) { throw new Error ( `sendXhr: request to ${ url } failed` ) ; } return result . response ; }