Skip to content

Commit 87d62d0

Browse files
committed
test(replay): Test user inactivity/activity
1 parent 93b695b commit 87d62d0

File tree

6 files changed

+340
-1
lines changed

6 files changed

+340
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
window.Replay = new Sentry.Replay({
5+
flushMinDelay: 500,
6+
flushMaxDelay: 500,
7+
});
8+
9+
Sentry.init({
10+
dsn: 'https://[email protected]/1337',
11+
sampleRate: 0,
12+
replaysSessionSampleRate: 1.0,
13+
replaysOnErrorSampleRate: 0.0,
14+
debug: true,
15+
16+
integrations: [window.Replay],
17+
});
18+
19+
window.Replay._replay.timeouts = {
20+
sessionIdle: 300000, // default: 5min
21+
maxSessionLife: 2000, // this is usually 60min, but we want to test this with shorter times
22+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button onclick="console.log('Test log 1')" id="button1">Click me</button>
8+
<button onclick="console.log('Test log 2')" id="button2">Click me</button>
9+
</body>
10+
</html>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { expect } from '@playwright/test';
2+
import type { ReplayEvent } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../utils/fixtures';
5+
import { envelopeRequestParser } from '../../../utils/helpers';
6+
import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates';
7+
import {
8+
getFullRecordingSnapshots,
9+
getIncrementalRecordingSnapshots,
10+
getReplaySnapshot,
11+
normalize,
12+
shouldSkipReplayTest,
13+
waitForReplayRequest,
14+
} from '../../../utils/replayHelpers';
15+
16+
// Session should expire after 2s - keep in sync with init.js
17+
const SESSION_TIMEOUT = 2000;
18+
19+
sentryTest('handles an inactive session', async ({ getLocalTestPath, page }) => {
20+
if (shouldSkipReplayTest()) {
21+
sentryTest.skip();
22+
}
23+
24+
const reqPromise0 = waitForReplayRequest(page, 0);
25+
26+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
27+
return route.fulfill({
28+
status: 200,
29+
contentType: 'application/json',
30+
body: JSON.stringify({ id: 'test-id' }),
31+
});
32+
});
33+
34+
const url = await getLocalTestPath({ testDir: __dirname });
35+
36+
await page.goto(url);
37+
38+
const replayEvent0 = envelopeRequestParser(await reqPromise0) as ReplayEvent;
39+
expect(replayEvent0).toEqual(getExpectedReplayEvent({}));
40+
41+
const fullSnapshots0 = getFullRecordingSnapshots(await reqPromise0);
42+
expect(fullSnapshots0.length).toEqual(1);
43+
const stringifiedSnapshot = normalize(fullSnapshots0[0]);
44+
expect(stringifiedSnapshot).toMatchSnapshot('snapshot-0.json');
45+
46+
await page.click('#button1');
47+
48+
// We wait for another segment 0
49+
const reqPromise1 = waitForReplayRequest(page, 0);
50+
51+
// Now we wait for the session timeout, nothing should be sent in the meanwhile
52+
await new Promise(resolve => setTimeout(resolve, SESSION_TIMEOUT));
53+
54+
// nothing happened because no activity/inactivity was detected
55+
const replay = await getReplaySnapshot(page);
56+
// @ts-ignore private api
57+
expect(replay._isEnabled).toEqual(true);
58+
// @ts-ignore private api
59+
expect(replay._isPaused).toEqual(false);
60+
61+
// Now we trigger a blur event, which should move the session to paused mode
62+
await page.evaluate(() => {
63+
window.dispatchEvent(new Event('blur'));
64+
});
65+
66+
const replay2 = await getReplaySnapshot(page);
67+
// @ts-ignore private api
68+
expect(replay2._isEnabled).toEqual(true);
69+
// @ts-ignore private api
70+
expect(replay2._isPaused).toEqual(true);
71+
72+
// Trigger an action, should re-start the recording
73+
await page.click('#button2');
74+
75+
const replay3 = await getReplaySnapshot(page);
76+
// @ts-ignore private api
77+
expect(replay3._isEnabled).toEqual(true);
78+
// @ts-ignore private api
79+
expect(replay3._isPaused).toEqual(false);
80+
81+
const replayEvent1 = envelopeRequestParser(await reqPromise1) as ReplayEvent;
82+
expect(replayEvent1).toEqual(getExpectedReplayEvent({}));
83+
84+
const fullSnapshots1 = getFullRecordingSnapshots(await reqPromise0);
85+
expect(fullSnapshots1.length).toEqual(1);
86+
const stringifiedSnapshot1 = normalize(fullSnapshots1[0]);
87+
expect(stringifiedSnapshot1).toMatchSnapshot('snapshot-1.json');
88+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"node": {
3+
"type": 0,
4+
"childNodes": [
5+
{
6+
"type": 1,
7+
"name": "html",
8+
"publicId": "",
9+
"systemId": "",
10+
"id": 2
11+
},
12+
{
13+
"type": 2,
14+
"tagName": "html",
15+
"attributes": {},
16+
"childNodes": [
17+
{
18+
"type": 2,
19+
"tagName": "head",
20+
"attributes": {},
21+
"childNodes": [
22+
{
23+
"type": 2,
24+
"tagName": "meta",
25+
"attributes": {
26+
"charset": "utf-8"
27+
},
28+
"childNodes": [],
29+
"id": 5
30+
}
31+
],
32+
"id": 4
33+
},
34+
{
35+
"type": 3,
36+
"textContent": "\n ",
37+
"id": 6
38+
},
39+
{
40+
"type": 2,
41+
"tagName": "body",
42+
"attributes": {},
43+
"childNodes": [
44+
{
45+
"type": 3,
46+
"textContent": "\n ",
47+
"id": 8
48+
},
49+
{
50+
"type": 2,
51+
"tagName": "button",
52+
"attributes": {
53+
"onclick": "console.log('Test log 1')",
54+
"id": "button1"
55+
},
56+
"childNodes": [
57+
{
58+
"type": 3,
59+
"textContent": "***** **",
60+
"id": 10
61+
}
62+
],
63+
"id": 9
64+
},
65+
{
66+
"type": 3,
67+
"textContent": "\n ",
68+
"id": 11
69+
},
70+
{
71+
"type": 2,
72+
"tagName": "button",
73+
"attributes": {
74+
"onclick": "console.log('Test log 2')",
75+
"id": "button2"
76+
},
77+
"childNodes": [
78+
{
79+
"type": 3,
80+
"textContent": "***** **",
81+
"id": 13
82+
}
83+
],
84+
"id": 12
85+
},
86+
{
87+
"type": 3,
88+
"textContent": "\n ",
89+
"id": 14
90+
},
91+
{
92+
"type": 3,
93+
"textContent": "\n\n",
94+
"id": 15
95+
}
96+
],
97+
"id": 7
98+
}
99+
],
100+
"id": 3
101+
}
102+
],
103+
"id": 1
104+
},
105+
"initialOffset": {
106+
"left": 0,
107+
"top": 0
108+
}
109+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"node": {
3+
"type": 0,
4+
"childNodes": [
5+
{
6+
"type": 1,
7+
"name": "html",
8+
"publicId": "",
9+
"systemId": "",
10+
"id": 2
11+
},
12+
{
13+
"type": 2,
14+
"tagName": "html",
15+
"attributes": {},
16+
"childNodes": [
17+
{
18+
"type": 2,
19+
"tagName": "head",
20+
"attributes": {},
21+
"childNodes": [
22+
{
23+
"type": 2,
24+
"tagName": "meta",
25+
"attributes": {
26+
"charset": "utf-8"
27+
},
28+
"childNodes": [],
29+
"id": 5
30+
}
31+
],
32+
"id": 4
33+
},
34+
{
35+
"type": 3,
36+
"textContent": "\n ",
37+
"id": 6
38+
},
39+
{
40+
"type": 2,
41+
"tagName": "body",
42+
"attributes": {},
43+
"childNodes": [
44+
{
45+
"type": 3,
46+
"textContent": "\n ",
47+
"id": 8
48+
},
49+
{
50+
"type": 2,
51+
"tagName": "button",
52+
"attributes": {
53+
"onclick": "console.log('Test log 1')",
54+
"id": "button1"
55+
},
56+
"childNodes": [
57+
{
58+
"type": 3,
59+
"textContent": "***** **",
60+
"id": 10
61+
}
62+
],
63+
"id": 9
64+
},
65+
{
66+
"type": 3,
67+
"textContent": "\n ",
68+
"id": 11
69+
},
70+
{
71+
"type": 2,
72+
"tagName": "button",
73+
"attributes": {
74+
"onclick": "console.log('Test log 2')",
75+
"id": "button2"
76+
},
77+
"childNodes": [
78+
{
79+
"type": 3,
80+
"textContent": "***** **",
81+
"id": 13
82+
}
83+
],
84+
"id": 12
85+
},
86+
{
87+
"type": 3,
88+
"textContent": "\n ",
89+
"id": 14
90+
},
91+
{
92+
"type": 3,
93+
"textContent": "\n\n",
94+
"id": 15
95+
}
96+
],
97+
"id": 7
98+
}
99+
],
100+
"id": 3
101+
}
102+
],
103+
"id": 1
104+
},
105+
"initialOffset": {
106+
"left": 0,
107+
"top": 0
108+
}
109+
}

rollup/plugins/bundlePlugins.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ export function makeTerserPlugin() {
116116
'_driver',
117117
'_initStorage',
118118
'_support',
119-
// We want to keept he _replay and _isEnabled variable unmangled to enable integration tests to access it
119+
// We want to keept some replay fields unmangled to enable integration tests to access them
120120
'_replay',
121121
'_isEnabled',
122+
'_isPaused',
122123
// We also can't mangle rrweb private fields when bundling rrweb in the replay CDN bundles
123124
'_cssText',
124125
// We want to keep the _integrations variable unmangled to send all installed integrations from replay

0 commit comments

Comments
 (0)