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

  1. Keep the predicate minimal — usually just check page.url() against an expected substring
  2. Wrap risky operations inside the predicate with try/catch and return false so the wait continues instead of failing
  3. 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

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


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)