grafana/k6 · error

parsing waitForElementState options: %w

Error message

parsing waitForElementState options: %w

What it means

Thrown synchronously by ElementHandle.waitForElementState() when its options fail to parse. ElementHandleWaitForElementStateOptions contains only timeout, parsed with a lenient coercion that always returns nil in common/element_handle_options.go, so this wrap is defensive and effectively unreachable in current k6. An invalid state string (first argument) is validated later inside the promise, not by this parse.

Source

Thrown at internal/js/modules/k6/browser/browser/element_handle_mapping.go:285

				return nil, fmt.Errorf("parsing type options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, eh.Type(text, popts) //nolint:wrapcheck
			}), nil
		},
		"uncheck": func(opts sobek.Value) (*sobek.Promise, error) {
			popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout())
			if err := popts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing uncheck options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, eh.Uncheck(popts) //nolint:wrapcheck
			}), nil
		},
		"waitForElementState": func(state string, opts sobek.Value) (*sobek.Promise, error) {
			popts := common.NewElementHandleWaitForElementStateOptions(eh.DefaultTimeout())
			if err := popts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing waitForElementState options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, eh.WaitForElementState(state, popts) //nolint:wrapcheck
			}), nil
		},
		"waitForSelector": func(selector string, opts sobek.Value) (*sobek.Promise, error) {
			popts := common.NewFrameWaitForSelectorOptions(eh.DefaultTimeout())
			if err := popts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing waitForSelector %q options: %w", selector, err)
			}
			return promise(vu, func() (any, error) {
				eh, err := eh.WaitForSelector(selector, popts)
				if err != nil {
					return nil, err //nolint:wrapcheck
				}
				return mapElementHandle(vu, eh), nil
			}), nil
		},

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use { timeout: <ms number> } only
  2. Use a valid state string: 'visible', 'hidden', 'stable', 'enabled', or 'disabled'
  3. Upgrade k6 if this message appears; current Parse cannot fail
  4. Check the wrapped cause after the colon to locate the failing field

Example fix

// before
await el.waitForElementState('visible', { timeout: '10s' });

// after
await el.waitForElementState('visible', { timeout: 10000 });
Defensive patterns

Strategy: try-catch

Validate before calling

const STATES = new Set(['visible', 'hidden', 'stable', 'enabled', 'disabled']);
if (!STATES.has(state)) {
  throw new Error(`waitForElementState(): invalid state '${state}'; use visible|hidden|stable|enabled|disabled`);
}
if (opts?.timeout !== undefined && !Number.isFinite(Number(opts.timeout))) {
  throw new Error('waitForElementState(): timeout must be ms number');
}

Type guard

function isWaitForElementStateOpts(o) {
  if (o == null) return true;
  return Object.keys(o).every(k => k === 'timeout') &&
         (o.timeout === undefined || Number.isFinite(o.timeout));
}

Try / catch

try {
  await el.waitForElementState(state, opts);
} catch (e) {
  if (/parsing waitForElementState options/.test(String(e.message))) {
    console.log(`fix options: ${e.message}`); // current k6 cannot raise this
  } else if (/unexpected state/.test(String(e.message))) {
    console.log(`invalid state argument: ${e.message}`); // validated inside the promise
  } else throw e;
}

Prevention

When it happens

Trigger: In current code none: any timeout value coerces via ToInteger; unknown option keys are ignored. Older structural Parse builds could fail.

Common situations: Timeout passed as '10s' (coerced, leading to an unexpected wait duration); mistaking the in-promise error 'unexpected state: <state>' for an option-parse failure.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/aa5f565ed380147c. Report an issue: GitHub.