Skip to content

Commit e82bf79

Browse files
committed
add keypress tests
1 parent da0cffe commit e82bf79

File tree

8 files changed

+132
-3
lines changed

8 files changed

+132
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [Sentry.browserTracingIntegration()],
8+
tracesSampleRate: 1,
9+
debug: true,
10+
});
11+
12+
document.getElementById('btn1').addEventListener('click', () => {
13+
// Trigger navigation later than click, so the last click is more than 300ms ago
14+
setTimeout(() => {
15+
window.history.pushState({}, '', '/sub-page');
16+
17+
// then trigger redirect inside of this navigation, which should be detected as a redirect
18+
// because the last click was more than 300ms ago
19+
setTimeout(() => {
20+
window.history.pushState({}, '', '/sub-page-redirect');
21+
}, 100);
22+
}, 400);
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<button id="btn1">Trigger navigation</button>
7+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../../utils/fixtures';
3+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers';
4+
5+
sentryTest(
6+
'should create a navigation.redirect span if a keypress happened more than 300ms before navigation',
7+
async ({ getLocalTestUrl, page }) => {
8+
if (shouldSkipTracingTest()) {
9+
sentryTest.skip();
10+
}
11+
12+
const url = await getLocalTestUrl({ testDir: __dirname });
13+
14+
const pageloadRequestPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === 'pageload');
15+
const navigationRequestPromise = waitForTransactionRequest(
16+
page,
17+
event => event.contexts?.trace?.op === 'navigation',
18+
);
19+
20+
await page.goto(url);
21+
22+
await pageloadRequestPromise;
23+
24+
// Now trigger navigation, and then a redirect in the navigation
25+
await page.focus('#btn1');
26+
await page.keyboard.press('Enter');
27+
28+
const navigationRequest = envelopeRequestParser(await navigationRequestPromise);
29+
30+
expect(navigationRequest.contexts?.trace?.op).toBe('navigation');
31+
expect(navigationRequest.transaction).toEqual('/sub-page');
32+
33+
const spans = navigationRequest.spans || [];
34+
35+
expect(spans).toContainEqual(
36+
expect.objectContaining({
37+
op: 'navigation.redirect',
38+
description: '/sub-page-redirect',
39+
}),
40+
);
41+
},
42+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [Sentry.browserTracingIntegration()],
8+
tracesSampleRate: 1,
9+
debug: true,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
document.getElementById('btn1').addEventListener('click', () => {
2+
// trigger redirect immediately
3+
window.history.pushState({}, '', '/sub-page');
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<button id="btn1" type="button">Trigger navigation</button>
7+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../../utils/fixtures';
3+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers';
4+
5+
sentryTest(
6+
'should not create a navigation.redirect span if a keypress happened before navigation',
7+
async ({ getLocalTestUrl, page }) => {
8+
if (shouldSkipTracingTest()) {
9+
sentryTest.skip();
10+
}
11+
12+
const url = await getLocalTestUrl({ testDir: __dirname });
13+
14+
const pageloadRequestPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === 'pageload');
15+
const navigationRequestPromise = waitForTransactionRequest(
16+
page,
17+
event => event.contexts?.trace?.op === 'navigation',
18+
);
19+
20+
await page.goto(url);
21+
await page.focus('#btn1');
22+
await page.keyboard.press('Enter');
23+
24+
const pageloadRequest = envelopeRequestParser(await pageloadRequestPromise);
25+
// Ensure a navigation span is sent, too
26+
await navigationRequestPromise;
27+
28+
const spans = pageloadRequest.spans || [];
29+
30+
expect(spans).not.toContainEqual(
31+
expect.objectContaining({
32+
op: 'navigation.redirect',
33+
}),
34+
);
35+
},
36+
);

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
466466
}
467467

468468
if (detectRedirects && optionalWindowDocument) {
469-
const clickHandler = (): void => {
469+
const interactionHandler = (): void => {
470470
lastInteractionTimestamp = timestampInSeconds();
471471
};
472-
addEventListener('click', clickHandler, { capture: true });
473-
addEventListener('keydown', clickHandler, { capture: true, passive: true });
472+
addEventListener('click', interactionHandler, { capture: true });
473+
addEventListener('keydown', interactionHandler, { capture: true, passive: true });
474474
}
475475

476476
function maybeEndActiveSpan(): void {

0 commit comments

Comments
 (0)