-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(replay): Consider
window.open
for slow clicks
When a click triggers `window.open()`, do not consider it a slow click.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/browser-integration-tests/suites/replay/slowClick/windowOpen/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getCustomRecordingEvents, shouldSkipReplayTest, waitForReplayRequest } from '../../../../utils/replayHelpers'; | ||
|
||
sentryTest('window.open() is considered for slow click', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipReplayTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
const reqPromise0 = waitForReplayRequest(page, 0); | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
await reqPromise0; | ||
|
||
const reqPromise1 = waitForReplayRequest(page, (event, res) => { | ||
const { breadcrumbs } = getCustomRecordingEvents(res); | ||
|
||
return breadcrumbs.some(breadcrumb => breadcrumb.category === 'ui.slowClickDetected'); | ||
}); | ||
|
||
await page.click('#windowOpenButton'); | ||
|
||
const { breadcrumbs } = getCustomRecordingEvents(await reqPromise1); | ||
|
||
const slowClickBreadcrumbs = breadcrumbs.filter(breadcrumb => breadcrumb.category === 'ui.slowClickDetected'); | ||
|
||
expect(slowClickBreadcrumbs).toEqual([ | ||
{ | ||
category: 'ui.slowClickDetected', | ||
data: { | ||
endReason: 'window.open', | ||
node: { | ||
attributes: { | ||
id: 'windowOpenButton', | ||
}, | ||
id: expect.any(Number), | ||
tagName: 'button', | ||
textContent: '****** ****', | ||
}, | ||
nodeId: expect.any(Number), | ||
timeAfterClickMs: expect.any(Number), | ||
url: 'http://sentry-test.io/index.html', | ||
}, | ||
message: 'body > button#windowOpenButton', | ||
timestamp: expect.any(Number), | ||
}, | ||
]); | ||
|
||
expect(slowClickBreadcrumbs[0]?.data?.timeAfterClickMs).toBeGreaterThan(3000); | ||
expect(slowClickBreadcrumbs[0]?.data?.timeAfterClickMs).toBeLessThan(3100); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fill } from '@sentry/utils'; | ||
|
||
import { WINDOW } from '../../constants'; | ||
|
||
type WindowOpenHandler = () => void; | ||
|
||
let handlers: undefined | WindowOpenHandler[]; | ||
|
||
/** | ||
* Register a handler to be called when `window.open()` is called. | ||
* Returns a cleanup function. | ||
*/ | ||
export function onWindowOpen(cb: WindowOpenHandler): () => void { | ||
// Ensure to only register this once | ||
if (!handlers) { | ||
handlers = []; | ||
monkeyPatchWindowOpen(); | ||
} | ||
|
||
handlers.push(cb); | ||
|
||
return () => { | ||
const pos = handlers ? handlers.indexOf(cb) : -1; | ||
if (pos > -1) { | ||
(handlers as WindowOpenHandler[]).splice(pos, 1); | ||
} | ||
}; | ||
} | ||
|
||
function monkeyPatchWindowOpen(): void { | ||
fill(WINDOW, 'open', function (originalWindowOpen: () => void): () => void { | ||
return function (...args: unknown[]): void { | ||
if (handlers) { | ||
try { | ||
handlers.forEach(handler => handler()); | ||
} catch (e) { | ||
// ignore errors in here | ||
} | ||
} | ||
|
||
return originalWindowOpen.apply(WINDOW, args); | ||
}; | ||
}); | ||
} |