Skip to content

test(browser): Add integration tests for sessions. #4500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/integration-tests/suites/sessions/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '0.1',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<meta charset='utf-8' />
<title></title>
<script src='{{htmlWebpackPlugin.options.initialization}}'></script>
</head>
<body>
<a id='navigate' href="foo">Navigate</button>
<script src='{{htmlWebpackPlugin.options.subject}}'></script>
</body>
</html>
37 changes: 37 additions & 0 deletions packages/integration-tests/suites/sessions/start-session/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, Route } from '@playwright/test';

import { sentryTest } from '../../../utils/fixtures';
import { getCurrentSession } from '../../../utils/helpers';

sentryTest('should start a new session on pageload.', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });
const session = await getCurrentSession(page, url);

expect(session).toBeDefined();
expect(session.init).toBe(true);
expect(session.errors).toBe(0);
expect(session.status).toBe('ok');
});

sentryTest('should start a new session with navigation.', async ({ getLocalTestPath, page, browserName }) => {
// Navigations get CORS error on Firefox and WebKit as we're using `file://` protocol.
if (browserName !== 'chromium') {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });
await page.route('**/foo', (route: Route) => route.fulfill({ path: `${__dirname}/dist/index.html` }));

const initSession = await getCurrentSession(page, url);

await page.click('#navigate');

const newSession = await getCurrentSession(page);

expect(newSession).toBeDefined();
expect(newSession.init).toBe(true);
expect(newSession.errors).toBe(0);
expect(newSession.status).toBe('ok');
expect(newSession.sid).toBeDefined();
expect(initSession.sid).not.toBe(newSession.sid);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
document.getElementById('throw-error').addEventListener('click', () => {
throw new Error('test');
});

document.getElementById('capture-exception').addEventListener('click', () => {
Sentry.captureException('test');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<meta charset='utf-8' />
<title></title>
<script src='{{htmlWebpackPlugin.options.initialization}}'></script>
</head>
<body>
<button id='throw-error'>Throw Error</button>
<button id='capture-exception'>Capture Exception</button>
<script src='{{htmlWebpackPlugin.options.subject}}'></script>
</body>
</html>
35 changes: 35 additions & 0 deletions packages/integration-tests/suites/sessions/update-session/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../utils/fixtures';
import { getCurrentSession } from '../../../utils/helpers';

sentryTest('should update session when an error is thrown.', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });
const pageloadSession = await getCurrentSession(page, url);
const updatedSession = (await Promise.all([page.click('#throw-error'), getCurrentSession(page)]))[1];

expect(pageloadSession).toBeDefined();
expect(pageloadSession.init).toBe(true);
expect(pageloadSession.errors).toBe(0);
expect(updatedSession).toBeDefined();
expect(updatedSession.init).toBe(false);
expect(updatedSession.errors).toBe(1);
expect(updatedSession.status).toBe('ok');
expect(pageloadSession.sid).toBe(updatedSession.sid);
});

sentryTest('should update session when an exception is captured.', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });

const pageloadSession = await getCurrentSession(page, url);
const updatedSession = (await Promise.all([page.click('#capture-exception'), getCurrentSession(page)]))[1];

expect(pageloadSession).toBeDefined();
expect(pageloadSession.init).toBe(true);
expect(pageloadSession.errors).toBe(0);
expect(updatedSession).toBeDefined();
expect(updatedSession.init).toBe(false);
expect(updatedSession.errors).toBe(1);
expect(updatedSession.status).toBe('ok');
expect(pageloadSession.sid).toBe(updatedSession.sid);
});
14 changes: 13 additions & 1 deletion packages/integration-tests/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Page, Request } from '@playwright/test';
import { Event } from '@sentry/types';
import { Event, SessionContext } from '@sentry/types';

const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\//;
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
Expand Down Expand Up @@ -46,6 +46,17 @@ async function getSentryTransactionRequest(page: Page, url?: string): Promise<Ev
return (await getMultipleSentryTransactionRequests(page, 1, url))[0];
}

/**
* Get current Sentry session at the given URL, or the current page
*
* @param {Page} page
* @param {string} [url]
* @return {*} {Promise<SessionContext>}
*/
async function getCurrentSession(page: Page, url?: string): Promise<SessionContext> {
return (await getMultipleSentryTransactionRequests(page, 1, url))[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is not guaranteed to be a session right?

I think we should rename getMultipleSentryTransactionRequests -> getMultipleSentryEnvelopeRequests and directly call that in the tests. The tests can just handle picking the right request and asserting on it (they can even assert on the total amount of requests sent if needed).

Maybe we use getMultipleSentryEnvelopeRequests just for this PR, and then migrate the other test files in another PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and #4527 seems to solve the confusion. I'll update the PR with proper types after that gets merged, unless this one is urgent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing using those types internally yet, I would just make the changes here and we can come back and update this after.

}

/**
* Get Sentry events at the given URL, or the current page.
*
Expand Down Expand Up @@ -170,5 +181,6 @@ export {
getSentryRequest,
getSentryTransactionRequest,
getSentryEvents,
getCurrentSession,
injectScriptAndGetEvents,
};