Skip to content

Commit 1be2959

Browse files
authored
test: add getMultipleSentryRequests helper. (#4355)
1 parent 1a5a820 commit 1be2959

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

packages/integration-tests/utils/helpers.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Page } from '@playwright/test';
22
import { Event } from '@sentry/types';
33

4+
const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\//;
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
*/
2224
async function getSentryRequest(page: Page, url: string): Promise<Event> {
23-
const request = (await Promise.all([page.goto(url), page.waitForRequest(/\.sentry\.io\/api\//)]))[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

Comments
 (0)