{"record":{"id":"cd2127ba7917131d","repo":"grafana/k6","slug":"waiting-for-page-s-event-w","errorCode":null,"errorMessage":"waiting for page %s event: %w","messagePattern":"waiting for page (.+?) event: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":1963,"sourceCode":"\n\t\tfor _, next := range handlers {\n\t\t\tif !yield(next.handler) {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}\n}\n\n// waitForEvent subscribes to the given event and resolves once the predicate\n// determines an event should complete the wait or terminate with an error.\nfunc (p *Page) waitForEvent(\n\tctx context.Context,\n\teventName PageEventName,\n\tpredicate func(PageEvent) (bool, error),\n) (_ PageEvent, rerr error) {\n\tdefer func() {\n\t\tif rerr != nil {\n\t\t\trerr = fmt.Errorf(\"waiting for page %s event: %w\", eventName, rerr)\n\t\t}\n\t}()\n\n\ttype pageEventWaitResult struct {\n\t\tevent PageEvent\n\t\terr   error\n\t}\n\n\tvar (\n\t\tonce   sync.Once\n\t\tresult = make(chan pageEventWaitResult, 1)\n\t)\n\tid, err := p.addEventHandler(eventName, func(event PageEvent) error {\n\t\tok, perr := predicate(event)\n\t\tif perr == nil && !ok {\n\t\t\treturn nil\n\t\t}\n\t\t// We don't want to deadlock if another event frequently happens.","sourceCodeStart":1945,"sourceCodeEnd":1981,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L1945-L1981","documentation":"Page.waitForEvent wraps every failure of an event wait in 'waiting for page <event> event', including the event name and the underlying cause. waitForEvent subscribes to a page event (e.g. 'popup', 'load', 'domcontentloaded', 'console') and resolves when a predicate accepts it; the deferred wrap catches timeout expirations, context cancellation, and predicate errors alike.","triggerScenarios":"await page.waitForEvent('popup', { timeout: N }) where no matching popup/open fires within the timeout; page.waitForLoadState() waiting for a lifecycle event that never fires; the k6 test context being cancelled (test iteration ending) while the wait is pending; or the predicate itself returning an error.","commonSituations":"Waiting for a popup that a click only triggers under certain conditions (popup blockers, target=_blank links removed); waiting for 'networkidle' on pages with persistent connections (websockets, analytics heartbeats) so it never settles; timeouts set too low on slow CI environments.","solutions":["Read the wrapped cause after the event name — 'context deadline exceeded' means timeout; raise the timeout option.","For 'networkidle' waits on pages with long-lived connections, switch to waitUntil: 'load' or 'domcontentloaded'.","Ensure the action that should emit the event (click, submit) actually ran before/while waiting — combine with page.waitForEvent before the triggering action using Promise.all.","Verify popups are not blocked and the link really uses target=_blank or window.open.","If the whole test was aborted, look at k6 iteration/time budgets instead of the wait itself."],"exampleFix":"// before\nconst p = page.waitForEvent('popup', { timeout: 1000 });\nawait page.click('#opens-popup'); // popup wait may time out first\n\n// after\nconst [popup] = await Promise.all([\n  page.waitForEvent('popup', { timeout: 10_000 }),\n  page.click('#opens-popup'),\n]);","handlingStrategy":"try-catch","validationCode":"// pair the wait with the action that triggers the event so it cannot time out spuriously\n// (see tryCatchPattern)","typeGuard":null,"tryCatchPattern":"try {\n  const [popup] = await Promise.all([\n    page.waitForEvent('popup', { timeout: 10_000 }),\n    page.click('#open-window'),\n  ]);\n} catch (e) {\n  if (/waiting for page popup event.*deadline/i.test(String(e))) { /* popup never opened */ }\n  else throw e;\n}","preventionTips":["Always start the waitForEvent promise before (or with Promise.all around) the triggering action.","Prefer 'load'/'domcontentloaded' over 'networkidle' on pages with persistent sockets.","Set explicit generous timeouts on slow CI."],"tags":["k6","browser","wait-for-event","timeout","popup"],"backgroundTag":"wait-timeout","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}