grafana/k6 · error

resetting screenshot background color: %w

Error message

resetting screenshot background color: %w

What it means

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.

Source

Thrown at internal/js/modules/k6/browser/common/screenshotter.go:227

		Y:      doc.Y,
		Width:  doc.Width,
		Height: doc.Height,
		Scale:  scale,
	}
	if clip.Width > 0 && clip.Height > 0 {
		capture = capture.WithClip(clip)
	}

	// Capture screenshot
	buf, err = capture.Do(cdp.WithExecutor(s.ctx, sess))
	if err != nil {
		return nil, fmt.Errorf("capturing screenshot: %w", err)
	}

	if shouldSetDefaultBackground {
		action := emulation.SetDefaultBackgroundColorOverride()
		if err := action.Do(cdp.WithExecutor(s.ctx, sess)); err != nil {
			return nil, fmt.Errorf("resetting screenshot background color: %w", err)
		}
	}

	// Save screenshot capture to file
	if path != "" {
		if err := s.persister.Persist(s.ctx, path, bytes.NewBuffer(buf)); err != nil {
			return nil, fmt.Errorf("persisting screenshot: %w", err)
		}
	}

	return buf, nil
}

func getViewPortDimensions(ctx context.Context, sess session, logger *log.Logger) (float64, float64, float64, error) {
	visualViewportScale := 1.0
	visualViewportPageX, visualViewportPageY := 0.0, 0.0

	// Add clip region

View on GitHub (pinned to 93accf6570)

Solutions

  1. Serialize page operations — ensure nothing closes the page until screenshot() resolves.
  2. Treat as non-fatal: the image was captured; if your run logs it, catch and continue.
  3. Retry screenshot once if the session is still alive; otherwise skip.
  4. Avoid omitBackground if the page is about to be closed anyway.

Example fix

// before
await page.screenshot({ path: 's.png', omitBackground: true });
await page.close(); // usually fine, but any concurrent close races the reset

// after
const shot = page.screenshot({ path: 's.png', omitBackground: true });
await shot; // fully resolved (including reset) before closing
await page.close();
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.screenshot({ path: 's.png', omitBackground: true });
} catch (e) {
  if (String(e).includes('resetting screenshot background color')) {
    // image may already be captured; check file exists and continue
  } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/50847db8e4da8904. Report an issue: GitHub.