import { Page , Request } from '@playwright/test' ; import { Event } from '@sentry/types' ; const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\// ; const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\// ; const storeRequestParser = ( request ) : Event => JSON . parse ( ( request && request . postData ( ) ) || '' ) ; const envelopeRequestParser = ( request ) : Event => { const envelope = request ?. postData ( ) || '' ; return envelope . split ( '\n' ) . map ( line => JSON . parse ( line ) ) [ 2 ] ; } ; param { Page } page param { string } path return { * } async function runScriptInSandbox ( page , path ) { await page . addScriptTag ( { path } ) ; } param { Page } page param { string } url return { * } async function getSentryRequest ( page , url ? ) { return ( await getMultipleSentryRequests ( page , 1 , url ) ) [ 0 ] ; } param { Page } page param { string } url return { * } async function getSentryTransactionRequest ( page , url ? ) { return ( await getMultipleSentryTransactionRequests ( page , 1 , url ) ) [ 0 ] ; } param { Page } page param { string } url return { * } async function getSentryEvents ( page , url ? ) { if ( url ) { await page . goto ( url ) ; } const eventsHandle = await page . evaluateHandle < Array < Event > > ( 'window.events' ) ; return eventsHandle . jsonValue ( ) ; } param { Page } page param { number } count param { RegExp } urlRgx param { ( req : Request ) => Event } requestParser param { string } url return { * } async function getMultipleRequests ( page , count , urlRgx , requestParser , url ? ) { const requests = new Promise ( ( resolve , reject ) => { let reqCount = count ; const requestData = [ ] ; page . on ( 'request' , request => { if ( urlRgx . test ( request . url ( ) ) ) { try { reqCount -= 1 ; requestData . push ( requestParser ( request ) ) ; if ( reqCount === 0 ) { resolve ( requestData ) ; } } catch ( err ) { reject ( err ) ; } } } ) ; } ) ; if ( url ) { await page . goto ( url ) ; } return requests ; } param { Page } page param { number } count param { string } url return { * } async function getMultipleSentryRequests ( page , count , url ? ) { return getMultipleRequests ( page , count , storeUrlRegex , storeRequestParser , url ) ; } param { Page } page param { number } count param { string } url return { * } async function getMultipleSentryTransactionRequests ( page , count , url ? ) { return getMultipleRequests ( page , count , envelopeUrlRegex , envelopeRequestParser , url ) ; } param { Page } page param { string } url param { string } scriptPath return { * } async function injectScriptAndGetEvents ( page , url , scriptPath ) { await page . goto ( url ) ; await runScriptInSandbox ( page , scriptPath ) ; return await getSentryEvents ( page ) ; } export { runScriptInSandbox , getMultipleSentryRequests , getMultipleSentryTransactionRequests , getSentryRequest , getSentryTransactionRequest , getSentryEvents , injectScriptAndGetEvents , } ;