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,72 @@ 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+ /**
44+ * Wait and get multiple event requests at the given URL, or the current page
45+ *
46+ * @param {Page } page
47+ * @param {number } count
48+ * @param {string } url
49+ * @return {* } {Promise<Event>}
50+ */
51+ async function getMultipleSentryRequests ( page : Page , count : number , url ?: string ) : Promise < Event [ ] > {
52+ const requests : Promise < Event [ ] > = new Promise ( ( resolve , reject ) => {
53+ let reqCount = 0 ;
54+ const requestData : Event [ ] = [ ] ;
55+
56+ page . on ( 'request' , request => {
57+ if ( storeUrlRegex . test ( request . url ( ) ) ) {
58+ reqCount += 1 ;
59+ try {
60+ requestData . push ( JSON . parse ( ( request && request . postData ( ) ) || '' ) ) ;
61+
62+ if ( reqCount >= count - 1 ) {
63+ resolve ( requestData ) ;
64+ }
65+ } catch ( err ) {
66+ reject ( err ) ;
67+ }
68+ }
69+ } ) ;
70+ } ) ;
71+
72+ if ( url ) {
73+ await page . goto ( url ) ;
74+ }
75+
76+ return requests ;
77+ }
78+
79+ async function getSentryTransactionRequest ( page : Page , url ?: string ) : Promise < Array < Record < string , unknown > > > {
80+ const request = ( await Promise . all ( [ page . goto ( url || '#' ) , waitForSentryRequest ( page , 'transaction' ) ] ) ) [ 1 ] ;
81+
82+ // https://develop.sentry.dev/sdk/envelopes/
83+ const envelope = ( request ?. postData ( ) as string ) || '' ;
84+
85+ return envelope . split ( '\n' ) . map ( line => JSON . parse ( line ) ) ;
86+ }
2887/**
2988 * Get Sentry events at the given URL, or the current page.
3089 *
@@ -58,4 +117,12 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
58117 return await getSentryEvents ( page ) ;
59118}
60119
61- export { runScriptInSandbox , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
120+ export {
121+ runScriptInSandbox ,
122+ waitForSentryRequest ,
123+ getSentryRequest ,
124+ getMultipleSentryRequests ,
125+ getSentryTransactionRequest ,
126+ getSentryEvents ,
127+ injectScriptAndGetEvents ,
128+ } ;
0 commit comments