grafana/k6 · error

parsing wait for options

Error message

parsing wait for options: %w

What it means

Thrown by k6 browser's Locator.waitFor(options) when the options object fails FrameWaitForSelectorOptions.Parse. The dominant cause is an invalid 'state' value: the parser only accepts 'attached', 'detached', 'visible', or 'hidden' (common/frame_options.go:754-760) and returns `"<value>" is not a valid DOM state` for anything else, which this message wraps. 'timeout' and 'strict' are the other recognized keys.

Solutions

  1. Use one of the four valid states: locator.waitFor({ state: 'attached' | 'detached' | 'visible' | 'hidden' }).
  2. For enabled/disabled semantics, wait for the element then assert with locator.isEnabled() or waitFor({state:'visible'}) plus your own check.
  3. Verify the state string exactly (no quotes mismatch, no whitespace) when building options dynamically.
  4. Call with no argument to use the default state 'visible' with the default timeout.

Example fix

// before
locator.waitFor({ state: 'enabled', timeout: 5000 });

// after
await locator.waitFor({ state: 'visible', timeout: 5000 });
if (!(await locator.isEnabled())) throw new Error('element not enabled');
Defensive patterns

Strategy: validation

Validate before calling

const VALID_STATES = new Set(['attached', 'detached', 'visible', 'hidden']);
function waitForOpts(state, timeout) {
  if (state && !VALID_STATES.has(state)) throw new Error(`invalid waitFor state: ${state}`);
  return state ? { state, timeout } : { timeout };
}
await locator.waitFor(waitForOpts(cfg.state, 5000));

Type guard

function isWaitForState(s) {
  return s == null || ['attached', 'detached', 'visible', 'hidden'].includes(s);
}

Try / catch

try { await locator.waitFor(opts); } catch (e) { if (/is not a valid DOM state|parsing wait for options/.test(e.message)) { /* log the state value and correct it */ } throw e; }

Prevention

When it happens

Trigger: Calling locator.waitFor({ state: 'enabled' }) or { state: 'disabled' } — states Playwright's waitForSelector never supported but often assumed; also 'stable', 'visible ' (trailing whitespace), or any typo like 'visable'. A non-object options value (BigInt, Symbol) that fails ToObject also lands here.

Common situations: Porting waitFor logic from Puppeteer (waitForSelector with different state vocabulary) or copying assertions code that uses elementHandle.isEnabled patterns; mismatches between Playwright docs examples and the subset k6 validates.

Related errors


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

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/locator_mapping.go:428

				return nil, fmt.Errorf("parsing locator tap options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.Tap(copts) //nolint:wrapcheck
			}), nil
		},
		"dispatchEvent": func(typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) {
			popts, err := parseFrameDispatchEventOptions(rt, opts, lo.DefaultTimeout())
			if err != nil {
				return nil, fmt.Errorf("parsing locator dispatch event options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.DispatchEvent(typ, exportArg(eventInit), popts) //nolint:wrapcheck
			}), nil
		},
		"waitFor": func(opts sobek.Value) (*sobek.Promise, error) {
			copts, err := parseFrameWaitForSelectorOptions(rt, opts, lo.Timeout())
			if err != nil {
				return nil, fmt.Errorf("parsing wait for options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.WaitFor(copts) //nolint:wrapcheck
			}), nil
		},
	}
}

func parseLocatorOptions(rt *sobek.Runtime, opts sobek.Value) *common.LocatorOptions {
	if k6common.IsNullish(opts) {
		return nil
	}

	var popts common.LocatorOptions

	obj := opts.ToObject(rt)
	for _, k := range obj.Keys() {
		switch k {

View on GitHub (pinned to 8d06114777)