{"record":{"id":"ffaaf83cd4e70882","repo":"grafana/k6","slug":"waitforevent-timed-out-after-v","errorCode":null,"errorMessage":"waitForEvent timed out after %v","messagePattern":"waitForEvent timed out after (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":383,"sourceCode":"\t}\n\n\tevCancelCtx, evCancelFn := context.WithCancel(b.ctx)\n\tdefer evCancelFn() // This will remove the event handler once we return from here.\n\n\tchEvHandler := make(chan Event)\n\tch := make(chan any)\n\terrCh := make(chan error)\n\n\tgo b.runWaitForEventHandler(evCancelCtx, chEvHandler, predicateFn, ch, errCh)\n\n\tb.on(evCancelCtx, []string{EventBrowserContextPage}, chEvHandler)\n\n\tselect {\n\tcase <-b.ctx.Done():\n\t\treturn nil, ContextErr(b.ctx) //nolint:wrapcheck\n\tcase <-time.After(timeout):\n\t\tb.logger.Debugf(\"BrowserContext:WaitForEvent:timeout\", \"bctxid:%v event:%q\", b.id, event)\n\t\treturn nil, fmt.Errorf(\"waitForEvent timed out after %v\", timeout)\n\tcase evData := <-ch:\n\t\tb.logger.Debugf(\"BrowserContext:WaitForEvent:evData\", \"bctxid:%v event:%q\", b.id, event)\n\t\treturn evData, nil\n\tcase err := <-errCh:\n\t\tb.logger.Debugf(\"BrowserContext:WaitForEvent:err\", \"bctxid:%v event:%q, err:%v\", b.id, event, err)\n\t\treturn nil, err\n\t}\n}\n\n// runWaitForEventHandler can work with a nil predicateFn. If predicateFn is\n// nil it will return the response straight away.\nfunc (b *BrowserContext) runWaitForEventHandler(\n\tctx context.Context,\n\tchEvHandler chan Event, predicateFn func(p *Page) (bool, error),\n\tout chan<- any, errOut chan<- error,\n) {\n\tb.logger.Debugf(\"BrowserContext:runWaitForEventHandler:go():starts\", \"bctxid:%v\", b.id)\n\tdefer b.logger.Debugf(\"BrowserContext:runWaitForEventHandler:go():returns\", \"bctxid:%v\", b.id)","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L365-L401","documentation":"waitForEvent('page', ...) ran the full timeout without a new page satisfying the condition: either no EventBrowserContextPage fired in the context during the window, or pages were created but the predicate never returned true (a predicate error returns a different error). The context remains usable — only this wait failed. Context cancellation is reported separately via ContextErr.","triggerScenarios":"Waiting for a popup/new tab that never opens: the click that should trigger it didn't happen or was blocked; the page opens in a DIFFERENT browser context; the predicate keeps returning false (e.g. wrong URL match); timeout shorter than the app's real popup delay.","commonSituations":"OAuth/social-login popups that stopped opening after a UI change; popup blockers or target=_blank links handled in the same tab; flaky CI where the site is slow and the default timeout is too small; predicates matching on a URL the app no longer uses.","solutions":["Pass a larger explicit timeout: ctx.waitForEvent('page', null, 30000) or raise the default with ctx.setDefaultTimeout(30000)","Make sure the action that opens the new page (click/submit) actually runs, and runs in the same context","Fix the predicate: log the actual page.url() inside it and compare against the real URL","Verify the site still opens a new tab rather than navigating in the same one"],"exampleFix":"// before\nconst popup = await ctx.waitForEvent('page', (p) => p.url().includes('login'), 1000);\n\n// after\nctx.setDefaultTimeout(30000);\nconst popupPromise = ctx.waitForEvent('page', (p) => p.url().includes('login'), 30000);\nawait page.locator('#open-popup').click(); // trigger AFTER starting the wait\nconst popup = await popupPromise;","handlingStrategy":"try-catch","validationCode":"// start the wait BEFORE the triggering action, with an explicit, generous timeout\nconst popupPromise = ctx.waitForEvent('page', (p) => p.url().includes('/oauth'), 30000);\nawait page.locator('#login').click();\nconst popup = await popupPromise;","typeGuard":null,"tryCatchPattern":"try {\n  const popup = await ctx.waitForEvent('page', predicate, 30000);\n} catch (e) {\n  if (String(e.message).includes('timed out')) {\n    // no qualifying page appeared: verify the trigger ran and the popup opens in THIS context\n    throw new Error(`no new page within timeout; popup blocked or wrong context: ${e.message}`);\n  }\n  throw e;\n}","preventionTips":["Always start waitForEvent before the click/navigation that opens the page","Give popups generous timeouts in CI (30s+), or raise ctx.setDefaultTimeout","Confirm the site opens a new tab in the same browser context your wait is on","Keep predicates cheap URL checks that eventually become true"],"tags":["browser","wait-for-event","timeout","popup"],"backgroundTag":"event-wait-timeout","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}