{"record":{"id":"50847db8e4da8904","repo":"grafana/k6","slug":"resetting-screenshot-background-color-w","errorCode":null,"errorMessage":"resetting screenshot background color: %w","messagePattern":"resetting screenshot background color: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":227,"sourceCode":"\t\tY:      doc.Y,\n\t\tWidth:  doc.Width,\n\t\tHeight: doc.Height,\n\t\tScale:  scale,\n\t}\n\tif clip.Width > 0 && clip.Height > 0 {\n\t\tcapture = capture.WithClip(clip)\n\t}\n\n\t// Capture screenshot\n\tbuf, err = capture.Do(cdp.WithExecutor(s.ctx, sess))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"capturing screenshot: %w\", err)\n\t}\n\n\tif shouldSetDefaultBackground {\n\t\taction := emulation.SetDefaultBackgroundColorOverride()\n\t\tif err := action.Do(cdp.WithExecutor(s.ctx, sess)); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"resetting screenshot background color: %w\", err)\n\t\t}\n\t}\n\n\t// Save screenshot capture to file\n\tif path != \"\" {\n\t\tif err := s.persister.Persist(s.ctx, path, bytes.NewBuffer(buf)); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"persisting screenshot: %w\", err)\n\t\t}\n\t}\n\n\treturn buf, nil\n}\n\nfunc getViewPortDimensions(ctx context.Context, sess session, logger *log.Logger) (float64, float64, float64, error) {\n\tvisualViewportScale := 1.0\n\tvisualViewportPageX, visualViewportPageY := 0.0, 0.0\n\n\t// Add clip region","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/screenshotter.go#L209-L245","documentation":"After a successful PNG capture with omitBackground:true, the screenshotter resets the default background color override (Emulation.setDefaultBackgroundColorOverride with no args). This error means the reset command failed — the screenshot bytes exist but the page is left with a transparent background override.","triggerScenarios":"Session becoming invalid in the window between capture and reset: page.close() racing the screenshot, browser shutdown, WebSocket drop. Purely a post-capture cleanup failure.","commonSituations":"Calling page.close() immediately after await page.screenshot({omitBackground:true}) from another task; k6 teardown cancelling the context mid-screenshot; flaky CDP connection at the end of a test.","solutions":["Serialize page operations — ensure nothing closes the page until screenshot() resolves.","Treat as non-fatal: the image was captured; if your run logs it, catch and continue.","Retry screenshot once if the session is still alive; otherwise skip.","Avoid omitBackground if the page is about to be closed anyway."],"exampleFix":"// before\nawait page.screenshot({ path: 's.png', omitBackground: true });\nawait page.close(); // usually fine, but any concurrent close races the reset\n\n// after\nconst shot = page.screenshot({ path: 's.png', omitBackground: true });\nawait shot; // fully resolved (including reset) before closing\nawait page.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.screenshot({ path: 's.png', omitBackground: true });\n} catch (e) {\n  if (String(e).includes('resetting screenshot background color')) {\n    // image may already be captured; check file exists and continue\n  } else { throw e; }\n}","preventionTips":["Await the screenshot fully before page.close().","Treat post-capture reset failures as non-fatal.","Avoid omitBackground on pages about to be torn down."],"tags":["k6","browser","screenshot","cdp","cleanup","omit-background"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}