11import { Page } 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+
46/**
57 * Run script at the given path inside the test environment.
68 *
@@ -20,7 +22,7 @@ async function runScriptInSandbox(page: Page, path: string): Promise<void> {
2022 * @return {* } {Promise<Event>}
2123 */
2224async 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 ] ;
2426
2527 return JSON . parse ( ( request && request . postData ( ) ) || '' ) ;
2628}
@@ -41,6 +43,42 @@ async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>>
4143 return eventsHandle . jsonValue ( ) ;
4244}
4345
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+
4482/**
4583 * Manually inject a script into the page of given URL.
4684 * 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
5896 return await getSentryEvents ( page ) ;
5997}
6098
61- export { runScriptInSandbox , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
99+ export { runScriptInSandbox , getMultipleSentryRequests , getSentryRequest , getSentryEvents , injectScriptAndGetEvents } ;
0 commit comments