grafana/k6 · error
predicate function failed
Error message
predicate function failed: %w
What it means
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.
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
Example fix
// before
const popup = await ctx.waitForEvent('page', (p) => {
return p.$('#ready') !== null; // throws on about:blank before navigation
});
// after
const popup = await ctx.waitForEvent('page', (p) =>
p.url().includes('/dashboard') // cheap URL check
);
await popup.locator('#ready').waitFor(); // assert after the wait Defensive patterns
Strategy: try-catch
Validate before calling
// keep the predicate minimal and non-throwing: URL check only
const popup = await ctx.waitForEvent('page', (p) =>
p.url().includes('/dashboard'));
// do DOM assertions after the wait resolves
await popup.locator('#ready').waitFor(); Try / catch
const popup = await ctx.waitForEvent('page', (p) => {
try {
return p.url().includes('/dashboard'); // never throw from the predicate
} catch {
return false; // page not ready: keep waiting instead of failing
}
}); Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- executing waitForEvent predicate
- "handler" argument cannot be nil
- incorrect event , is the only event supported
- predicate function is not callable
- predicate must be a function
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/cffc0e85aa13fec5.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:433
continue
}
b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage", "bctxid:%v", b.id)
p, ok := ev.data.(*Page)
if !ok {
errOut <- fmt.Errorf("on create page event failed to return a page: %w", k6error.ErrFatal)
return
}
if predicateFn == nil {
b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:return", "bctxid:%v", b.id)
out <- p
return
}
retVal, err := predicateFn(p)
if err != nil {
errOut <- fmt.Errorf("predicate function failed: %w", err)
return
}
if retVal {
b.logger.Debugf(
"BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:predicateFn:return",
"bctxid:%v", b.id,
)
out <- p
return
}
}
}
}
func (b *BrowserContext) getSession(id target.SessionID) *Session {
return b.browser.conn.getSession(id)
}View on GitHub (pinned to 01ffac6f24)