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,36 @@ 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
+ 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
+ }
28
51
/**
29
52
* Get Sentry events at the given URL, or the current page.
30
53
*
@@ -58,4 +81,11 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
58
81
return await getSentryEvents ( page ) ;
59
82
}
60
83
61
- export { runScriptInSandbox , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
84
+ export {
85
+ runScriptInSandbox ,
86
+ waitForSentryRequest ,
87
+ getSentryRequest ,
88
+ getSentryTransactionRequest ,
89
+ getSentryEvents ,
90
+ injectScriptAndGetEvents ,
91
+ } ;
0 commit comments