1
- import { Page } from '@playwright/test' ;
1
+ import { Page , Request } from '@playwright/test' ;
2
2
import { Event } from '@sentry/types' ;
3
3
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
+
4
9
/**
5
10
* Run script at the given path inside the test environment.
6
11
*
@@ -13,18 +18,72 @@ async function runScriptInSandbox(page: Page, path: string): Promise<void> {
13
18
}
14
19
15
20
/**
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
17
32
*
18
33
* @param {Page } page
19
34
* @param {string } url
20
35
* @return {* } {Promise<Event>}
21
36
*/
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 ] ;
24
39
25
40
return JSON . parse ( ( request && request . postData ( ) ) || '' ) ;
26
41
}
27
42
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
+ }
28
87
/**
29
88
* Get Sentry events at the given URL, or the current page.
30
89
*
@@ -58,4 +117,12 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
58
117
return await getSentryEvents ( page ) ;
59
118
}
60
119
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