{"record":{"id":"295c91d98aa5f6f7","repo":"grafana/k6","slug":"parsing-element-handle-screenshot-options-w","errorCode":null,"errorMessage":"parsing element handle screenshot options: %w","messagePattern":"parsing element handle screenshot options: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/element_handle_mapping.go","lineNumber":176,"sourceCode":"\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err //nolint:wrapcheck\n\t\t\t\t}\n\t\t\t\treturn mapFrame(vu, f), nil\n\t\t\t})\n\t\t},\n\t\t\"press\": func(key string, opts sobek.Value) (*sobek.Promise, error) {\n\t\t\tpopts := common.NewElementHandlePressOptions(eh.DefaultTimeout())\n\t\t\tif err := popts.Parse(vu.Context(), opts); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing press %q options: %w\", key, err)\n\t\t\t}\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, eh.Press(key, popts) //nolint:wrapcheck\n\t\t\t}), nil\n\t\t},\n\t\t\"screenshot\": func(opts sobek.Value) (*sobek.Promise, error) {\n\t\t\tpopts := common.NewElementHandleScreenshotOptions(eh.Timeout())\n\t\t\tif err := popts.Parse(vu.Context(), opts); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing element handle screenshot options: %w\", err)\n\t\t\t}\n\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\tbb, err := eh.Screenshot(popts, vu.filePersister)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err //nolint:wrapcheck\n\t\t\t\t}\n\n\t\t\t\tab := rt.NewArrayBuffer(bb)\n\n\t\t\t\treturn &ab, nil\n\t\t\t}), nil\n\t\t},\n\t\t\"scrollIntoViewIfNeeded\": func(opts sobek.Value) (*sobek.Promise, error) {\n\t\t\tpopts := common.NewElementHandleBaseOptions(eh.DefaultTimeout())\n\t\t\tif err := popts.Parse(vu.Context(), opts); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing scrollIntoViewIfNeeded options: %w\", err)\n\t\t\t}","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/browser/element_handle_mapping.go#L158-L194","documentation":"Thrown synchronously by ElementHandle.screenshot() when its options fail to parse. ElementHandleScreenshotOptions (path, type, quality, omitBackground, timeout) is parsed with lenient coercions in common/element_handle_options.go and always returns nil, so this wrap is defensive in current k6. Notably an invalid type value (e.g. 'gif') is silently ignored and PNG is used, so format mistakes do not raise this error either.","triggerScenarios":"In current code none: path/type/quality/omitBackground/timeout all coerce leniently; an unknown type string is skipped and .jpg/.jpeg suffixes on path infer JPEG. Older structural Parse builds could fail the whole-object export.","commonSituations":"Assuming screenshot() validates type or quality (it does not: quality only matters for JPEG, invalid type falls back to PNG); scripts written against old xk6-browser; passing screenshot data as options instead of using the returned buffer.","solutions":["Use only { path, type: 'jpeg'|'png', quality, omitBackground, timeout } with matching types","If this message appears, upgrade k6 and retry with an empty options object to isolate it","Check the wrapped cause after the colon for the failing field in your build","For JPEG quality control set type: 'jpeg' explicitly or end path with .jpg/.jpeg"],"exampleFix":"// before\nawait el.screenshot({ path: 'shot.png', type: 'gif', quality: 'high' });\n\n// after\nawait el.screenshot({ path: 'shot.png', type: 'png', quality: 90, timeout: 10000 });","handlingStrategy":"try-catch","validationCode":"if (opts?.type !== undefined && !['png', 'jpeg'].includes(opts.type)) {\n  throw new Error(`screenshot(): invalid type '${opts.type}' (k6 silently falls back to PNG)`);\n}\nif (opts?.quality !== undefined && !Number.isInteger(opts.quality)) {\n  throw new Error('screenshot(): quality must be an integer');\n}","typeGuard":"function isScreenshotOpts(o) {\n  if (o == null) return true;\n  return (o.path === undefined || typeof o.path === 'string') &&\n         (o.type === undefined || ['png', 'jpeg'].includes(o.type)) &&\n         (o.quality === undefined || Number.isInteger(o.quality)) &&\n         (o.omitBackground === undefined || typeof o.omitBackground === 'boolean') &&\n         (o.timeout === undefined || Number.isFinite(o.timeout));\n}","tryCatchPattern":"try {\n  const buf = await el.screenshot(opts);\n} catch (e) {\n  if (/parsing element handle screenshot options/.test(String(e.message))) {\n    console.log(`fix screenshot options: ${e.message}`);\n  } else throw e; // real failures (timeout, detached element) occur inside the promise\n}","preventionTips":["Do not rely on parse errors for validation: invalid type strings are silently ignored and PNG is used","Set type: 'jpeg' explicitly (or a .jpg path) when you need quality to apply","Current k6 cannot raise this parse error; pin k6 versions in CI to avoid behavior drift"],"tags":["k6","browser","element-handle","screenshot","option-parsing"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}