|
| 1 | +import type { Route } from '@playwright/test'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import type { Event } from '@sentry/types'; |
| 4 | + |
| 5 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/browser'; |
| 6 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 7 | +import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; |
| 8 | + |
| 9 | +sentryTest( |
| 10 | + 'should capture long animation frame for top-level script.', |
| 11 | + async ({ browserName, getLocalTestPath, page }) => { |
| 12 | + // Long animation frames only work on chrome |
| 13 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 14 | + sentryTest.skip(); |
| 15 | + } |
| 16 | + |
| 17 | + await page.route('**/path/to/script.js', (route: Route) => |
| 18 | + route.fulfill({ path: `${__dirname}/assets/script.js` }), |
| 19 | + ); |
| 20 | + |
| 21 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 22 | + |
| 23 | + const promise = getFirstSentryEnvelopeRequest<Event>(page); |
| 24 | + |
| 25 | + await page.goto(url); |
| 26 | + |
| 27 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 28 | + |
| 29 | + const eventData = await promise; |
| 30 | + |
| 31 | + const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long-animation-frame')); |
| 32 | + |
| 33 | + expect(uiSpans?.length).toEqual(1); |
| 34 | + |
| 35 | + const [topLevelUISpan] = uiSpans || []; |
| 36 | + expect(topLevelUISpan).toEqual( |
| 37 | + expect.objectContaining({ |
| 38 | + op: 'ui.long-animation-frame', |
| 39 | + description: 'Main UI thread blocked', |
| 40 | + parent_span_id: eventData.contexts?.trace?.span_id, |
| 41 | + data: { |
| 42 | + 'code.filepath': 'https://example.com/path/to/script.js', |
| 43 | + 'browser.script.source_char_position': 0, |
| 44 | + 'browser.script.invoker': 'https://example.com/path/to/script.js', |
| 45 | + 'browser.script.invoker_type': 'classic-script', |
| 46 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'ui.long-animation-frame', |
| 47 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.metrics', |
| 48 | + }, |
| 49 | + }), |
| 50 | + ); |
| 51 | + const start = topLevelUISpan.start_timestamp ?? 0; |
| 52 | + const end = topLevelUISpan.timestamp ?? 0; |
| 53 | + const duration = end - start; |
| 54 | + |
| 55 | + expect(duration).toBeGreaterThanOrEqual(0.1); |
| 56 | + expect(duration).toBeLessThanOrEqual(0.15); |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +sentryTest( |
| 61 | + 'should capture long animation frame for event listener.', |
| 62 | + async ({ browserName, getLocalTestPath, page }) => { |
| 63 | + // Long animation frames only work on chrome |
| 64 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 65 | + sentryTest.skip(); |
| 66 | + } |
| 67 | + |
| 68 | + await page.route('**/path/to/script.js', (route: Route) => |
| 69 | + route.fulfill({ path: `${__dirname}/assets/script.js` }), |
| 70 | + ); |
| 71 | + |
| 72 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 73 | + |
| 74 | + const promise = getFirstSentryEnvelopeRequest<Event>(page); |
| 75 | + |
| 76 | + await page.goto(url); |
| 77 | + |
| 78 | + // trigger long animation frame function |
| 79 | + await page.getByRole('button').click(); |
| 80 | + |
| 81 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 82 | + |
| 83 | + const eventData = await promise; |
| 84 | + |
| 85 | + const uiSpans = eventData.spans?.filter(({ op }) => op?.startsWith('ui.long-animation-frame')); |
| 86 | + |
| 87 | + expect(uiSpans?.length).toEqual(2); |
| 88 | + |
| 89 | + // ignore the first ui span (top-level long animation frame) |
| 90 | + const [, eventListenerUISpan] = uiSpans || []; |
| 91 | + |
| 92 | + expect(eventListenerUISpan).toEqual( |
| 93 | + expect.objectContaining({ |
| 94 | + op: 'ui.long-animation-frame', |
| 95 | + description: 'Main UI thread blocked', |
| 96 | + parent_span_id: eventData.contexts?.trace?.span_id, |
| 97 | + data: { |
| 98 | + 'browser.script.invoker': 'BUTTON#clickme.onclick', |
| 99 | + 'browser.script.invoker_type': 'event-listener', |
| 100 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'ui.long-animation-frame', |
| 101 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.metrics', |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + const start = eventListenerUISpan.start_timestamp ?? 0; |
| 106 | + const end = eventListenerUISpan.timestamp ?? 0; |
| 107 | + const duration = end - start; |
| 108 | + |
| 109 | + expect(duration).toBeGreaterThanOrEqual(0.1); |
| 110 | + expect(duration).toBeLessThanOrEqual(0.15); |
| 111 | + }, |
| 112 | +); |
0 commit comments