grafana/k6 · error

executing waitForEvent predicate

Error message

executing waitForEvent predicate: %w

What it means

Thrown while page.waitForEvent(..., { predicate }) is running: the JavaScript predicate function itself raised an exception when called with the mapped page event. The mapping layer executes the predicate on the Sobek runtime inside the task queue; any throw is wrapped with this message and aborts the wait.

Solutions

  1. Wrap the predicate body in try/catch and return false on failure so one bad event does not kill the wait.
  2. Null-check optional accessors before use: if (!e.response()) return false;.
  3. Verify every method called on the event exists in the k6 browser API mapping (Request, Response, ConsoleMessage).
  4. Log the event inside the predicate during debugging to see which shape is actually delivered.

Example fix

// before
await page.waitForEvent('response', { predicate: r => r.request().method() === 'GET' && r.status() === 200 });

// after
await page.waitForEvent('response', {
  predicate: r => {
    try { return r.request().method() === 'GET' && r.status() === 200; }
    catch { return false; }
  },
});
Defensive patterns

Strategy: try-catch

Try / catch

await page.waitForEvent('response', {
  predicate: (e) => {
    try { return e.url().includes('/api') && e.status() < 400; }
    catch { return false; } // skip events whose shape we can't inspect
  },
});

Prevention

When it happens

Trigger: A predicate that accesses properties not present on the event object (e.g. e.response().status() when the request has no response yet, or using fields of a ConsoleMessage where a Request was delivered); calling a non-existent method; referencing an undefined variable inside the predicate.

Common situations: Predicates written for one event type reused for another (a Request predicate passed while waiting for 'console'); calling e.response() without a null check on requestfailed events; assuming Playwright's full event API surface exists on the mapped k6 object.

Related errors


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/213e2ebc15124306. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/page_mapping.go:716

				return nil, fmt.Errorf("parsing waitForEvent options: %w", err)
			}

			ctx := vu.Context()
			tq := vu.get(ctx, p.TargetID())

			return promise(vu, func() (any, error) {
				rpe, err := p.WaitForEvent(event, popts, func(pe common.PageEvent) (bool, error) {
					if fn == nil {
						return true, nil
					}
					return queueTask(ctx, tq, func() (bool, error) {
						m, err := mapPageEvent(vu, pe)
						if err != nil {
							return false, err
						}
						v, err := fn(sobek.Undefined(), vu.Runtime().ToValue(m))
						if err != nil {
							return false, fmt.Errorf("executing waitForEvent predicate: %w", err)
						}
						return v.ToBoolean(), nil
					})()
				})
				if err != nil {
					return nil, fmt.Errorf("waiting for page event %q: %w", event, err)
				}
				return mapPageEvent(vu, rpe)
			}), nil
		},
		"workers": func() *sobek.Object {
			workers := p.Workers()
			mws := make([]mapping, 0, len(workers))
			for _, w := range workers {
				mw := mapWorker(vu, w)
				mws = append(mws, mw)
			}
			return rt.ToValue(mws).ToObject(rt)

View on GitHub (pinned to 8d06114777)