grafana/k6 · error

capturing screenshot: %w

Error message

capturing screenshot: %w

What it means

The core Page.captureScreenshot CDP command failed. By this point options were validated, the background override (if any) applied and the clip computed — the browser itself refused or failed to produce the image bytes.

Source

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

	scale := 1.0
	if viewport != nil {
		scale = visualViewportScale
	}
	clip = &cdppage.Viewport{
		X:      doc.X,
		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
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Verify the page is open and stable: await page.goto(url); await page.waitForLoadState('load'); before capturing, and never close concurrently.
  2. Reduce capture cost: avoid fullPage on gigantic pages, lower viewport size, or capture JPEG instead of PNG.
  3. Raise resource limits for the browser process (container memory) or screenshot fewer pages in parallel.
  4. Retry the capture once — renderer crashes are frequently one-off; if persistent, set K6_BROWSER_ARGS/--disable-gpu or switch headless shell version, and report to grafana/k6 if reproducible.

Example fix

// before
await page.screenshot({ path: 'big.png', fullPage: true }); // renderer OOM on huge page

// after
await page.screenshot({ path: 'big.jpg', fullPage: true, type: 'jpeg', quality: 80 });
Defensive patterns

Strategy: retry

Validate before calling

if (page.isClosed()) throw new Error('page closed; cannot capture');

Try / catch

async function shot(p, opts, n = 2) {
  for (let i = 0; i < n; i++) {
    try { return await p.screenshot(opts); }
    catch (e) { if (i === n - 1 || !/capturing screenshot/i.test(String(e))) throw e; await sleep(1000); }
  }
}

Prevention

When it happens

Trigger: Renderer crash ('Aw, Snap' / SIGSEGV in chrome), page or target closed while capturing, GPU/compositing failures in the execution environment, session cancelled by k6 shutdown or iteration timeout, or captures on a tab that was backgrounded/discarded by the OS.

Common situations: Very large fullPage screenshots exhausting memory in CI containers (renderer OOM-killed); headless chrome without --disable-gpu or with software rasterizer bugs; screenshot racing page.close(); too many concurrent pages capturing at once.

Related errors


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