1- import { Page } from '@playwright/test' ;
1+ import { Page , Request } from '@playwright/test' ;
22import { Event } from '@sentry/types' ;
33
4+ const storeUrlRegex = / \. s e n t r y \. i o \/ a p i \/ \d + \/ s t o r e \/ / ;
5+ const envelopeUrlRegex = / \. s e n t r y \. i o \/ a p i \/ \d + \/ e n v e l o p e \/ / ;
6+
7+ type SentryRequestType = 'event' | 'transaction' ;
8+
49/**
510 * Run script at the given path inside the test environment.
611 *
@@ -13,18 +18,36 @@ async function runScriptInSandbox(page: Page, path: string): Promise<void> {
1318}
1419
1520/**
16- * Wait and get Sentry's request sending the event at the given URL
21+ * Wait and get Sentry's request sending the event.
22+ *
23+ * @param {Page } page
24+ * @returns {* } {Promise<Request>}
25+ */
26+ async function waitForSentryRequest ( page : Page , requestType : SentryRequestType = 'event' ) : Promise < Request > {
27+ return page . waitForRequest ( requestType === 'event' ? storeUrlRegex : envelopeUrlRegex ) ;
28+ }
29+
30+ /**
31+ * Wait and get Sentry's request sending the event at the given URL, or the current page
1732 *
1833 * @param {Page } page
1934 * @param {string } url
2035 * @return {* } {Promise<Event>}
2136 */
22- async function getSentryRequest ( page : Page , url : string ) : Promise < Event > {
23- const request = ( await Promise . all ( [ page . goto ( url ) , page . waitForRequest ( / \. s e n t r y \. i o \/ a p i \/ / ) ] ) ) [ 1 ] ;
37+ async function getSentryRequest ( page : Page , url ? : string ) : Promise < Event > {
38+ const request = ( await Promise . all ( [ page . goto ( url || '#' ) , waitForSentryRequest ( page ) ] ) ) [ 1 ] ;
2439
2540 return JSON . parse ( ( request && request . postData ( ) ) || '' ) ;
2641}
2742
43+ async function getSentryTransactionRequest ( page : Page , url ?: string ) : Promise < Array < Record < string , unknown > > > {
44+ const request = ( await Promise . all ( [ page . goto ( url || '#' ) , waitForSentryRequest ( page , 'transaction' ) ] ) ) [ 1 ] ;
45+
46+ // https://develop.sentry.dev/sdk/envelopes/
47+ const envelope = ( request ?. postData ( ) as string ) || '' ;
48+
49+ return envelope . split ( '\n' ) . map ( line => JSON . parse ( line ) ) ;
50+ }
2851/**
2952 * Get Sentry events at the given URL, or the current page.
3053 *
@@ -58,4 +81,11 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
5881 return await getSentryEvents ( page ) ;
5982}
6083
61- export { runScriptInSandbox , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
84+ export {
85+ runScriptInSandbox ,
86+ waitForSentryRequest ,
87+ getSentryRequest ,
88+ getSentryTransactionRequest ,
89+ getSentryEvents ,
90+ injectScriptAndGetEvents ,
91+ } ;
0 commit comments