grafana/k6 · error

options[ ]: expected int, got %T

Error message

options[%v]: expected int, got %T

What it means

Thrown by extractSelectOptionFromMap (browser/mapping.go:161) while parsing a selectOption descriptor: the 'index' key was present but its value is neither int nor int64. Position-based selection requires { index: <integer> }; strings and floats fall into the default branch and are rejected with the key name in the message.

Solutions

  1. Pass an integer literal: { index: 2 }.
  2. Coerce at the boundary: { index: Number(data.idx) } and check Number.isInteger first.
  3. Truncate computed floats: { index: Math.trunc(pos) } when the fractional part is noise.
  4. Prefer value/label matching over index when the dataset is string-typed.

Example fix

// before
await page.selectOption('#sel', { index: data.position }); // "2"

// after
const idx = Number(data.position);
if (!Number.isInteger(idx)) throw new Error(`bad index: ${data.position}`);
await page.selectOption('#sel', { index: idx });
Defensive patterns

Strategy: type-guard

Validate before calling

const opt = { ...raw };
if ('index' in opt) {
  const n = Number(opt.index);
  if (!Number.isInteger(n)) throw new Error(`index must be an integer, got ${opt.index}`);
  opt.index = n;
}
await page.selectOption('#sel', opt);

Type guard

function hasIntegerIndex(o) {
  return !('index' in o) || (typeof o.index === 'number' && Number.isInteger(o.index));
}

Try / catch

try { await page.selectOption('#sel', opt); } catch (e) { if (/options\[index\]: expected int/.test(e.message)) { await page.selectOption('#sel', { ...opt, index: parseInt(opt.index, 10) }); } else throw e; }

Prevention

When it happens

Trigger: page.selectOption('#sel', { index: '2' }) (string from JSON or user input) or { index: 2.5 } / { index: 2.0 } when the value arrives as float64 — Sobek only exports whole numbers written as integer literals as int64, so floats parsed from JSON can fail even when they look whole.

Common situations: Index values read from JSON/CSV files (parsed as string or float); indices computed by arithmetic that produced a float; Playwright porting where index came through a serialization boundary.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/mapping.go:169

			*opt.Value = v
		case "label":
			opt.Label = new(string)

			v, ok := raw.(string)
			if !ok {
				return nil, fmt.Errorf("options[%v]: expected string, got %T", k, raw)
			}
			*opt.Label = v
		case "index":
			opt.Index = new(int64)

			switch raw := raw.(type) {
			case int:
				*opt.Index = int64(raw)
			case int64:
				*opt.Index = raw
			default:
				return nil, fmt.Errorf("options[%v]: expected int, got %T", k, raw)
			}
		}
	}

	return opt, nil
}

View on GitHub (pinned to 8d06114777)