{"record":{"id":"729e87f52aaac85a","repo":"grafana/k6","slug":"width-must-be-a-number-got-s","errorCode":null,"errorMessage":"width must be a number, got %s","messagePattern":"width must be a number, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/page_mapping.go","lineNumber":1206,"sourceCode":"func parseSize(rt *sobek.Runtime, opts sobek.Value) (*common.Size, error) {\n\tsize := &common.Size{}\n\tif k6common.IsNullish(opts) {\n\t\treturn size, nil\n\t}\n\n\tobj := opts.ToObject(rt)\n\tfor _, k := range obj.Keys() {\n\t\tv := obj.Get(k)\n\t\tif k6common.IsNullish(v) {\n\t\t\tcontinue\n\t\t}\n\t\tswitch k {\n\t\tcase \"width\":\n\t\t\tswitch v.ExportType().Kind() {\n\t\t\tcase reflect.Int64, reflect.Float64:\n\t\t\t\tsize.Width = v.ToFloat()\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"width must be a number, got %s\", v.ExportType().Kind())\n\t\t\t}\n\t\tcase \"height\":\n\t\t\tswitch v.ExportType().Kind() {\n\t\t\tcase reflect.Int64, reflect.Float64:\n\t\t\t\tsize.Height = v.ToFloat()\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"height must be a number, got %s\", v.ExportType().Kind())\n\t\t\t}\n\t\t}\n\t}\n\n\treturn size, nil\n}\n\n// parsePageEmulateMediaOptions parses the page emulate media options from a Sobek value.\n//\n//nolint:unparam\nfunc parsePageEmulateMediaOptions(","sourceCodeStart":1188,"sourceCodeEnd":1224,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/browser/page_mapping.go#L1188-L1224","documentation":"Thrown by parseSize when the 'width' key of page.setViewportSize(size) is present but its value's runtime kind is neither Int64 nor Float64. Numeric strings, booleans, objects and arrays are rejected — only plain JavaScript numbers are accepted (nullish values are skipped).","triggerScenarios":"page.setViewportSize({ width: '1280', height: 720 }) (width as string); width from an environment variable or CSV without Number() conversion; { width: null } handled, but { width: 'auto' } or boolean values fail.","commonSituations":"Reading viewport dimensions from k6 options/env (__ENV.VIEWPORT_W) which always yields strings; sharing a config with a UI framework that serializes numbers to strings; partial objects copied from Playwright where width was allowed to be null.","solutions":["Convert to numbers before the call: { width: Number(__ENV.VIEWPORT_W), height: Number(__ENV.VIEWPORT_H) }.","Prefer browser.newContext({ viewport: { width: 1280, height: 720 } }) with literal numbers when the size is static.","Validate with Number.isFinite before calling and fall back to a sane default.","Never pass CSS-style strings like '1280px'."],"exampleFix":"// before\nawait page.setViewportSize({ width: __ENV.W, height: __ENV.H });\n\n// after\nawait page.setViewportSize({ width: Number(__ENV.W), height: Number(__ENV.H) });","handlingStrategy":"type-guard","validationCode":"const w = Number(size.width);\nif (!Number.isFinite(w)) throw new TypeError(`width must be a number, got ${typeof size.width}`);\nawait page.setViewportSize({ width: w, height: Number(size.height) });","typeGuard":"const isNumericSize = (s) => s == null || ((s.width === undefined || Number.isFinite(Number(s.width))) && (s.height === undefined || Number.isFinite(Number(s.height))));","tryCatchPattern":null,"preventionTips":["Coerce env/config strings with Number() before setViewportSize.","Define viewport once with numeric literals in newContext options when static.","Validate external config at script startup and fail fast."],"tags":["k6","browser","viewport","type-error","argument-validation"],"backgroundTag":"argument-type-mismatch","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}