1
1
import { Page } 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
+
4
6
/**
5
7
* Run script at the given path inside the test environment.
6
8
*
@@ -20,7 +22,7 @@ async function runScriptInSandbox(page: Page, path: string): Promise<void> {
20
22
* @return {* } {Promise<Event>}
21
23
*/
22
24
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 ] ;
25
+ const request = ( await Promise . all ( [ page . goto ( url ) , page . waitForRequest ( storeUrlRegex ) ] ) ) [ 1 ] ;
24
26
25
27
return JSON . parse ( ( request && request . postData ( ) ) || '' ) ;
26
28
}
@@ -41,6 +43,42 @@ async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>>
41
43
return eventsHandle . jsonValue ( ) ;
42
44
}
43
45
46
+ /**
47
+ * Wait and get multiple event requests at the given URL, or the current page
48
+ *
49
+ * @param {Page } page
50
+ * @param {number } count
51
+ * @param {string } url
52
+ * @return {* } {Promise<Event>}
53
+ */
54
+ async function getMultipleSentryRequests ( page : Page , count : number , url ?: string ) : Promise < Event [ ] > {
55
+ const requests : Promise < Event [ ] > = new Promise ( ( resolve , reject ) => {
56
+ let reqCount = 0 ;
57
+ const requestData : Event [ ] = [ ] ;
58
+
59
+ page . on ( 'request' , request => {
60
+ if ( storeUrlRegex . test ( request . url ( ) ) ) {
61
+ reqCount += 1 ;
62
+ try {
63
+ requestData . push ( JSON . parse ( ( request && request . postData ( ) ) || '' ) ) ;
64
+
65
+ if ( reqCount >= count - 1 ) {
66
+ resolve ( requestData ) ;
67
+ }
68
+ } catch ( err ) {
69
+ reject ( err ) ;
70
+ }
71
+ }
72
+ } ) ;
73
+ } ) ;
74
+
75
+ if ( url ) {
76
+ await page . goto ( url ) ;
77
+ }
78
+
79
+ return requests ;
80
+ }
81
+
44
82
/**
45
83
* Manually inject a script into the page of given URL.
46
84
* This function is useful to create more complex test subjects that can't be achieved by pre-built pages.
@@ -58,4 +96,4 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
58
96
return await getSentryEvents ( page ) ;
59
97
}
60
98
61
- export { runScriptInSandbox , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
99
+ export { runScriptInSandbox , getMultipleSentryRequests , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
0 commit comments