{"record":{"id":"cffc0e85aa13fec5","repo":"grafana/k6","slug":"predicate-function-failed-w","errorCode":null,"errorMessage":"predicate function failed: %w","messagePattern":"predicate function failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":433,"sourceCode":"\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tb.logger.Debugf(\"BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage\", \"bctxid:%v\", b.id)\n\t\t\tp, ok := ev.data.(*Page)\n\t\t\tif !ok {\n\t\t\t\terrOut <- fmt.Errorf(\"on create page event failed to return a page: %w\", k6error.ErrFatal)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif predicateFn == nil {\n\t\t\t\tb.logger.Debugf(\"BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:return\", \"bctxid:%v\", b.id)\n\t\t\t\tout <- p\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tretVal, err := predicateFn(p)\n\t\t\tif err != nil {\n\t\t\t\terrOut <- fmt.Errorf(\"predicate function failed: %w\", err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif retVal {\n\t\t\t\tb.logger.Debugf(\n\t\t\t\t\t\"BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:predicateFn:return\",\n\t\t\t\t\t\"bctxid:%v\", b.id,\n\t\t\t\t)\n\t\t\t\tout <- p\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc (b *BrowserContext) getSession(id target.SessionID) *Session {\n\treturn b.browser.conn.getSession(id)\n}","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L415-L451","documentation":"The predicate passed to waitForEvent('page', predicate) returned an error while evaluating a newly created page; the error is wrapped as 'predicate function failed' and sent to the caller, ending the wait. The predicate receives the new *Page and returns [bool, error] — any error it produces (including exceptions in JS) becomes this error.","triggerScenarios":"Inside the predicate: calling page methods that throw on a page still on about:blank (e.g. querying a selector that doesn't exist yet), referencing undefined variables, or doing heavier work than the intended boolean check.","commonSituations":"Predicates that eagerly evaluate DOM state on a freshly created page before navigation; predicates with typos accessing properties of null; copying the predicate body from page.waitForFunction examples where errors behave differently.","solutions":["Keep the predicate minimal — usually just check page.url() against an expected substring","Wrap risky operations inside the predicate with try/catch and return false so the wait continues instead of failing","Do DOM assertions AFTER waitForEvent resolves, on the returned page, not inside the predicate"],"exampleFix":"// before\nconst popup = await ctx.waitForEvent('page', (p) => {\n  return p.$('#ready') !== null; // throws on about:blank before navigation\n});\n\n// after\nconst popup = await ctx.waitForEvent('page', (p) =>\n  p.url().includes('/dashboard') // cheap URL check\n);\nawait popup.locator('#ready').waitFor(); // assert after the wait","handlingStrategy":"try-catch","validationCode":"// keep the predicate minimal and non-throwing: URL check only\nconst popup = await ctx.waitForEvent('page', (p) =>\n  p.url().includes('/dashboard'));\n// do DOM assertions after the wait resolves\nawait popup.locator('#ready').waitFor();","typeGuard":null,"tryCatchPattern":"const popup = await ctx.waitForEvent('page', (p) => {\n  try {\n    return p.url().includes('/dashboard'); // never throw from the predicate\n  } catch {\n    return false; // page not ready: keep waiting instead of failing\n  }\n});","preventionTips":["Predicates should only inspect page.url(); defer DOM queries until after the wait","Wrap any risky predicate logic in try/catch returning false","Never reference possibly-undefined closures inside the predicate"],"tags":["browser","wait-for-event","predicate","callback"],"backgroundTag":"callback-threw-exception","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"}