{"record":{"id":"0ac4220db25bd658","repo":"grafana/k6","slug":"options-expected-string-or-object-got-t","errorCode":null,"errorMessage":"options: expected string or object, got %T","messagePattern":"options: expected string or object, got %T","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/mapping.go","lineNumber":83,"sourceCode":"\n\t\tfor _, item := range sl {\n\t\t\tswitch item := item.(type) {\n\t\t\tcase string:\n\t\t\t\t// Strings will match values or labels\n\t\t\t\tvalOpt := common.SelectOption{Value: new(string)}\n\t\t\t\t*valOpt.Value = item\n\t\t\t\tlabelOpt := common.SelectOption{Label: new(string)}\n\t\t\t\t*labelOpt.Label = item\n\t\t\t\topts = append(opts, &valOpt, &labelOpt)\n\t\t\tcase map[string]any:\n\t\t\t\topt, err := extractSelectOptionFromMap(item)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\topts = append(opts, opt)\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"options: expected string or object, got %T\", item)\n\t\t\t}\n\t\t}\n\tcase reflect.Map:\n\t\tvar raw map[string]any\n\t\tif err := rt.ExportTo(values, &raw); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"options: expected object, got %T\", values)\n\t\t}\n\n\t\topt, err := extractSelectOptionFromMap(raw)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\topts = append(opts, opt)\n\tcase reflect.TypeFor[*common.ElementHandle]().Kind():\n\t\topts = append(opts, t.(*common.ElementHandle)) //nolint:forcetypeassert\n\tcase reflect.TypeFor[sobek.Object]().Kind():\n\t\tobj := values.ToObject(rt)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/browser/mapping.go#L65-L101","documentation":"Thrown by ConvertSelectOptionValues when an item inside the selectOption values array is neither a string nor a plain object. Each array element must be a string (matches value or label) or a map descriptor with value/label/index; anything else — number, boolean, null, nested array — rejects with 'options: expected string or object, got <type>', wrapped by the mapping as 'parsing select option values: ...'.","triggerScenarios":"selectOption([0, 2]) — raw indices instead of { index: n } descriptors; selectOption(['red', null]) — null item; selectOption([true]) or [['red']] — boolean/nested-array items.","commonSituations":"Data-driven tests mapping API responses straight into selectOption where the payload contains numbers or nulls; assuming zero-based indices can be passed raw; sloppy spread of optional values (...maybeValues) that can inject undefined/null.","solutions":["Wrap indices: selectOption([{ index: 0 }, { index: 2 }])","Filter null/undefined out of the array before the call","Map every item to a string or { value } / { label } / { index } descriptor explicitly","Validate external data shape before feeding it to the browser module"],"exampleFix":"// before\nawait page.locator('select.size').selectOption([0, null, 2]);\n\n// after\nawait page.locator('select.size').selectOption([{ index: 0 }, { index: 2 }]);","handlingStrategy":"type-guard","validationCode":"function sanitizeSelectItems(items) {\n  return items\n    .filter((it) => it !== null && it !== undefined)\n    .map((it) => {\n      if (typeof it === 'string') return it;\n      if (typeof it === 'number') return { index: it };\n      if (typeof it === 'object' && !Array.isArray(it)) return it;\n      throw new TypeError(`selectOption array items must be string or object, got ${typeof it}`);\n    });\n}","typeGuard":"function isSelectOptionItem(v) {\n  if (typeof v === 'string') return true;\n  if (typeof v !== 'object' || v === null || Array.isArray(v)) return false;\n  return (v.value === undefined || typeof v.value === 'string') &&\n         (v.label === undefined || typeof v.label === 'string') &&\n         (v.index === undefined || typeof v.index === 'number');\n}","tryCatchPattern":"try {\n  await locator.selectOption(values, opts);\n} catch (e) {\n  if (/expected string or object/.test(String(e.message))) {\n    throw new Error(`selectOption array may contain only strings or {value,label,index} objects: ${e.message}`);\n  }\n  throw e;\n}","preventionTips":["Wrap numeric indices as { index: n } before passing","Filter null/undefined from data-driven arrays","Map API payloads explicitly to strings or descriptors"],"tags":["k6","browser","selectoption","type-validation","array"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}