Skip to content

Commit a912187

Browse files
committed
Add helper.
1 parent b49aeb3 commit a912187

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

packages/integration-tests/utils/helpers.ts

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

4+
const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\//;
5+
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
6+
7+
type SentryRequestType = 'event' | 'transaction';
8+
49
/**
510
* Run script at the given path inside the test environment.
611
*
@@ -13,18 +18,36 @@ async function runScriptInSandbox(page: Page, path: string): Promise<void> {
1318
}
1419

1520
/**
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
1732
*
1833
* @param {Page} page
1934
* @param {string} url
2035
* @return {*} {Promise<Event>}
2136
*/
22-
async function getSentryRequest(page: Page, url: string): Promise<Event> {
23-
const request = (await Promise.all([page.goto(url), page.waitForRequest(/\.sentry\.io\/api\//)]))[1];
37+
async function getSentryRequest(page: Page, url?: string): Promise<Event> {
38+
const request = (await Promise.all([page.goto(url || '#'), waitForSentryRequest(page)]))[1];
2439

2540
return JSON.parse((request && request.postData()) || '');
2641
}
2742

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+
}
2851
/**
2952
* Get Sentry events at the given URL, or the current page.
3053
*
@@ -58,4 +81,11 @@ async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: str
5881
return await getSentryEvents(page);
5982
}
6083

61-
export { runScriptInSandbox, getSentryRequest, getSentryEvents, injectScriptAndGetEvents };
84+
export {
85+
runScriptInSandbox,
86+
waitForSentryRequest,
87+
getSentryRequest,
88+
getSentryTransactionRequest,
89+
getSentryEvents,
90+
injectScriptAndGetEvents,
91+
};

0 commit comments

Comments
 (0)