Skip to content

test(tracing): Add tests for backgroundtab. #4443

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 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Sentry from '@sentry/browser';
import { Integrations } from '@sentry/tracing';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [new Integrations.BrowserTracing({ idleTimeout: 9000 })],
tracesSampleRate: 1,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.getElementById('go-background').addEventListener('click', () => {
Object.defineProperty(document, 'hidden', { value: true, writable: true });
const ev = document.createEvent('Event');
ev.initEvent('visibilitychange');
document.dispatchEvent(ev);
});

document.getElementById('start-transaction').addEventListener('click', () => {
window.transaction = Sentry.startTransaction({ name: 'test-transaction' });
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(window.transaction));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script src="{{htmlWebpackPlugin.options.initialization}}"></script>
</head>
<body>
<button id="start-transaction">Start Transaction</button>
<button id="go-background">New Tab</button>
<script src="{{htmlWebpackPlugin.options.subject}}"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, JSHandle } from '@playwright/test';

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

async function getPropertyValue(handle: JSHandle, prop: string) {
return (await handle.getProperty(prop))?.jsonValue();
}

sentryTest('should finish a custom transaction when the page goes background', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });

const pageloadTransaction = await getSentryTransactionRequest(page, url);
expect(pageloadTransaction).toBeDefined();

await page.click('#start-transaction');
const transactionHandle = await page.evaluateHandle('window.transaction');

const id_before = await getPropertyValue(transactionHandle, 'span_id');
const name_before = await getPropertyValue(transactionHandle, 'name');
const status_before = await getPropertyValue(transactionHandle, 'status');
const tags_before = await getPropertyValue(transactionHandle, 'tags');

expect(name_before).toBe('test-transaction');
expect(status_before).toBeUndefined();
expect(tags_before).toStrictEqual({});

await page.click('#go-background');

const id_after = await getPropertyValue(transactionHandle, 'span_id');
const name_after = await getPropertyValue(transactionHandle, 'name');
const status_after = await getPropertyValue(transactionHandle, 'status');
const tags_after = await getPropertyValue(transactionHandle, 'tags');

expect(id_before).toBe(id_after);
expect(name_after).toBe(name_before);
expect(status_after).toBe('cancelled');
expect(tags_after).toStrictEqual({ finishReason: 'documentHidden', visibilitychange: 'document.hidden' });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
document.getElementById('go-background').addEventListener('click', () => {
Object.defineProperty(document, 'hidden', { value: true, writable: true });
const ev = document.createEvent('Event');
ev.initEvent('visibilitychange');
document.dispatchEvent(ev);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<script src='{{htmlWebpackPlugin.options.initialization}}'></script>
</head>
<body>
]
<button id='go-background'>New Tab</button>
<script src='{{htmlWebpackPlugin.options.subject}}'></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from '@playwright/test';

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

sentryTest('should finish pageload transaction when the page goes background', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });

await page.goto(url);

page.click('#go-background');

const pageloadTransaction = await getSentryTransactionRequest(page);

expect(pageloadTransaction.contexts?.trace.op).toBe('pageload');
expect(pageloadTransaction.contexts?.trace.status).toBe('cancelled');
expect(pageloadTransaction.contexts?.trace.tags).toMatchObject({
visibilitychange: 'document.hidden',
finishReason: 'documentHidden',
});
});