grafana/k6 · error

frame waitForFunction

Error message

frame waitForFunction: %w

What it means

Thrown by the waitForFunction mapping (browser/frame_mapping.go:438) when parseWaitForFunctionArgs fails. The concrete failing branch is FrameWaitForFunctionOptions.Parse: 'wrong polling option value: %q; possible values: "raf", "mutation" or number'. k6 accepts only the strings 'raf' and 'mutation' or a numeric polling interval; Playwright's polling: 'interval' and numeric strings like '100' are rejected.

Solutions

  1. Use a number for interval polling: { polling: 100 }
  2. Use the supported strings: { polling: 'raf' } or { polling: 'mutation' }
  3. Delete the polling key entirely (defaults to 'raf')
  4. Coerce config-sourced values: { polling: Number(cfg.interval) }

Example fix

// before
frame.waitForFunction(() => window.ready, { polling: 'interval', interval: 100 });
// after
frame.waitForFunction(() => window.ready, { polling: 100 });
Defensive patterns

Strategy: validation

Validate before calling

const POLLING = ['raf', 'mutation'];
function normalizePolling(o = {}) {
  const p = o.polling;
  if (p === undefined) return o;
  if (typeof p === 'number' && Number.isFinite(p) && p >= 0) return o;
  if (POLLING.includes(p)) return o;
  if (typeof p === 'string' && p.trim() !== '' && Number.isFinite(Number(p))) return { ...o, polling: Number(p) };
  throw new Error(`invalid polling: ${p}; use 'raf', 'mutation' or a number`);
}

Type guard

function isWaitForFunctionOptions(o) {
  if (o == null) return true;
  const p = o.polling;
  return p === undefined || p === 'raf' || p === 'mutation' || (typeof p === 'number' && Number.isFinite(p));
}

Try / catch

try { await frame.waitForFunction(fn, normalizePolling(opts)); } catch (e) { if (!String(e.message).includes('waitForFunction')) throw e; }

Prevention

When it happens

Trigger: frame.waitForFunction(() => window.ready, { polling: 'interval', interval: 100 }) (Playwright spelling); { polling: '100' } (interval as a string); explicitly passing polling: undefined/null as a present key (falls into the default reject branch).

Common situations: Porting Playwright examples verbatim; using the waitForFunction docs from Playwright instead of k6's; setting polling from environment/JSON config so the number arrives as a string.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of grafana/k6@8d06114777 (2026-09-14). Data as JSON: /api/errors/a8ce45a4d6a5ce61. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/frame_mapping.go:436

			}), nil
		},
		"uncheck": func(selector string, opts sobek.Value) (*sobek.Promise, error) {
			popts, err := parseFrameUncheckOptions(rt, opts, f.Timeout())
			if err != nil {
				return nil, fmt.Errorf("parsing frame uncheck options %q: %w", selector, err)
			}

			return promise(vu, func() (any, error) {
				return nil, f.Uncheck(selector, popts) //nolint:wrapcheck
			}), nil
		},
		"url": f.URL,
		"waitForFunction": func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) {
			js, popts, pargs, err := parseWaitForFunctionArgs(
				rt, f.Timeout(), pageFunc, opts, args...,
			)
			if err != nil {
				return nil, fmt.Errorf("frame waitForFunction: %w", err)
			}

			return promise(vu, func() (result any, reason error) {
				return f.WaitForFunction(js, popts, pargs...) //nolint:wrapcheck
			}), nil
		},
		"waitForLoadState": func(state string, opts sobek.Value) (*sobek.Promise, error) {
			popts, err := parseFrameWaitForLoadStateOptions(rt, opts, f.Timeout())
			if err != nil {
				return nil, fmt.Errorf("parsing waitForLoadState %q options: %w", state, err)
			}

			return promise(vu, func() (any, error) {
				return nil, f.WaitForLoadState(state, popts) //nolint:wrapcheck
			}), nil
		},
		"waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) {
			return mapWaitForNavigation(rt, vu, f, opts)

View on GitHub (pinned to 8d06114777)