{"record":{"id":"3d100b5131f641c5","repo":"grafana/k6","slug":"waiting-for-page-event-q-w","errorCode":null,"errorMessage":"waiting for page event %q: %w","messagePattern":"waiting for page event %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/page_mapping.go","lineNumber":722,"sourceCode":"\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\trpe, err := p.WaitForEvent(event, popts, func(pe common.PageEvent) (bool, error) {\n\t\t\t\t\tif fn == nil {\n\t\t\t\t\t\treturn true, nil\n\t\t\t\t\t}\n\t\t\t\t\treturn queueTask(ctx, tq, func() (bool, error) {\n\t\t\t\t\t\tm, err := mapPageEvent(vu, pe)\n\t\t\t\t\t\tif err != nil {\n\t\t\t\t\t\t\treturn false, err\n\t\t\t\t\t\t}\n\t\t\t\t\t\tv, err := fn(sobek.Undefined(), vu.Runtime().ToValue(m))\n\t\t\t\t\t\tif err != nil {\n\t\t\t\t\t\t\treturn false, fmt.Errorf(\"executing waitForEvent predicate: %w\", err)\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn v.ToBoolean(), nil\n\t\t\t\t\t})()\n\t\t\t\t})\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, fmt.Errorf(\"waiting for page event %q: %w\", event, err)\n\t\t\t\t}\n\t\t\t\treturn mapPageEvent(vu, rpe)\n\t\t\t}), nil\n\t\t},\n\t\t\"workers\": func() *sobek.Object {\n\t\t\tworkers := p.Workers()\n\t\t\tmws := make([]mapping, 0, len(workers))\n\t\t\tfor _, w := range workers {\n\t\t\t\tmw := mapWorker(vu, w)\n\t\t\t\tmws = append(mws, mw)\n\t\t\t}\n\t\t\treturn rt.ToValue(mws).ToObject(rt)\n\t\t},\n\t}\n\tmaps[\"$\"] = func(selector string) *sobek.Promise {\n\t\treturn promise(vu, func() (any, error) {\n\t\t\teh, err := p.Query(selector)\n\t\t\tif err != nil {","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/browser/page_mapping.go#L704-L740","documentation":"Thrown when the underlying p.WaitForEvent call fails after the options parsed and the wait started. It wraps the raw failure: most commonly a timeout waiting for the event within p.Timeout() (the page/action timeout), but also browser or context closing mid-wait, or an error escaping the predicate.","triggerScenarios":"page.waitForEvent('response', { timeout: 1000 }) when no matching network exchange happens in time; the page navigating away or being closed before the event fires; a predicate that never returns true; running against a slow site under load where responses exceed the default 30s action timeout.","commonSituations":"Waiting for events that never occur because the selector/navigation step before them failed silently; tight timeouts in CI; heavy pages under k6 load where responses are slower than scripted; browser context closed by script end while an event wait is in flight.","solutions":["Increase the timeout: pass { timeout: 60000 } or raise the default with page.setDefaultTimeout(60000) / newContext({ actionTimeout }).","Verify the triggering action actually happens (the click/navigation that should produce the event) and that the event name matches a mapped event.","If using a predicate, confirm it can return true (test it against a captured event).","Catch the rejection and fail the iteration with a descriptive k6 check message instead of an unhandled promise rejection."],"exampleFix":"// before\nconst res = await page.waitForEvent('response');\n\n// after\ntry {\n  const res = await page.waitForEvent('response', { timeout: 60000 });\n} catch (e) {\n  check(null, { 'response event received': () => false });\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const ev = await page.waitForEvent('response', { timeout: 60000 });\n} catch (e) {\n  if (/waiting for page event/.test(e.message)) {\n    check(null, { 'event arrived in time': () => false });\n  }\n  throw e;\n}","preventionTips":["Set realistic timeouts (page.setDefaultTimeout / newContext actionTimeout) instead of relying on the 30s default under load.","Confirm the action that should trigger the event succeeded before waiting for it.","Verify predicates can return true by logging captured events once."],"tags":["k6","browser","page","waitforevent","timeout"],"backgroundTag":"operation-timed-out","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}