|
| 1 | +import { Page } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Helper function that waits for the initial pageload to complete. |
| 6 | + * |
| 7 | + * This function |
| 8 | + * - loads the given route ("/" by default) |
| 9 | + * - waits for SvelteKit's hydration |
| 10 | + * - waits for the pageload transaction to be sent (doesn't assert on it though) |
| 11 | + * |
| 12 | + * Useful for tests that test outcomes of _navigations_ after an initial pageload. |
| 13 | + * Waiting on the pageload transaction excludes edge cases where navigations occur |
| 14 | + * so quickly that the pageload idle transaction is still active. This might lead |
| 15 | + * to cases where the routing span would be attached to the pageload transaction |
| 16 | + * and hence eliminates a lot of flakiness. |
| 17 | + * |
| 18 | + */ |
| 19 | +export async function waitForInitialPageload( |
| 20 | + page: Page, |
| 21 | + opts?: { route?: string; parameterizedRoute?: string; debug?: boolean }, |
| 22 | +) { |
| 23 | + const route = opts?.route ?? '/'; |
| 24 | + const txnName = opts?.parameterizedRoute ?? route; |
| 25 | + const debug = opts?.debug ?? false; |
| 26 | + |
| 27 | + const clientPageloadTxnEventPromise = waitForTransaction('sveltekit-2-svelte-5', txnEvent => { |
| 28 | + debug && |
| 29 | + console.log({ |
| 30 | + txn: txnEvent?.transaction, |
| 31 | + op: txnEvent.contexts?.trace?.op, |
| 32 | + trace: txnEvent.contexts?.trace?.trace_id, |
| 33 | + span: txnEvent.contexts?.trace?.span_id, |
| 34 | + parent: txnEvent.contexts?.trace?.parent_span_id, |
| 35 | + }); |
| 36 | + |
| 37 | + return txnEvent?.transaction === txnName && txnEvent.contexts?.trace?.op === 'pageload'; |
| 38 | + }); |
| 39 | + |
| 40 | + await Promise.all([ |
| 41 | + page.goto(route), |
| 42 | + // the test app adds the "hydrated" class to the body when hydrating |
| 43 | + page.waitForSelector('body.hydrated'), |
| 44 | + // also waiting for the initial pageload txn so that later navigations don't interfere |
| 45 | + clientPageloadTxnEventPromise, |
| 46 | + ]); |
| 47 | + |
| 48 | + debug && console.log('hydrated'); |
| 49 | +} |
0 commit comments