grafana/k6 · error

getting original viewport size

Error message

getting original viewport size: %w

What it means

First step of elementHandle.screenshot(): the screenshotter calls originalViewportSize(page) to know the current viewport (evaluating window dimensions if the page has none recorded), and this wraps its failure. The underlying error is either the window.innerWidth/innerHeight evaluation failing or its conversion failing — the same causes as the 'getting viewport dimensions'/'unpacking window size' errors, but reached from the element-screenshot entry point.

Solutions

  1. Check the wrapped cause (context destroyed / target closed) and screenshot while the page is definitely alive.
  2. Wait for the element and lifecycle state before capturing.
  3. Launch with an explicit viewport to skip the evaluation path.
  4. Stabilize the browser environment (memory, shm) if the renderer crashed.

Example fix

// before
await page.locator('#chart').screenshot({ path: 'chart.png' }); // right after goto

// after
const chart = page.locator('#chart');
await chart.waitFor({ state: 'visible' });
await chart.screenshot({ path: 'chart.png' });
Defensive patterns

Strategy: validation

Validate before calling

if (page.isClosed()) throw new Error('page closed');
await el.waitFor({ state: 'visible' }); // element present before element screenshot

Prevention

When it happens

Trigger: await elementHandle.screenshot() on a viewport:null context while the page is navigating, closing, or crashed, so the viewport fallback evaluation fails.

Common situations: Element screenshots late in a test while the browser tears down; crashed renderer in CI; screenshots racing redirects.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-09-20). Data as JSON: /api/errors/a5eb4d5212c76d22. Report an issue: GitHub.

Appendix: source

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

			"Screenshotter::screenshot",
			"chrome browser returned nil on page.getLayoutMetrics, falling back to defaults for cssVisualViewport "+
				"(scale: %v, pageX: %v, pageY: %v)."+
				"This is non-standard behavior, if possible please report this issue (with a reproducible script) "+
				"to the https://github.com/grafana/k6/issues/new.",
			visualViewportScale, visualViewportPageX, visualViewportPageY,
		)
	}

	return visualViewportScale, visualViewportPageX, visualViewportPageY, nil
}

func (s *screenshotter) screenshotElement(
	h *ElementHandle, opts *ElementHandleScreenshotOptions,
) (buf []byte, err error) {
	format := opts.Format
	viewportSize, err := s.originalViewportSize(h.frame.page)
	if err != nil {
		return nil, fmt.Errorf("getting original viewport size: %w", err)
	}
	originalSize := h.frame.page.emulatedSize

	err = h.waitAndScrollIntoViewIfNeeded(h.ctx, false, true, opts.Timeout)
	if err != nil {
		return nil, fmt.Errorf("scrolling element into view: %w", err)
	}

	bbox, err := h.boundingBox()
	if err != nil {
		return nil, fmt.Errorf("node is either not visible or not an HTMLElement: %w", err)
	}
	if bbox.Width <= 0 {
		return nil, fmt.Errorf("node has 0 width")
	}
	if bbox.Height <= 0 {
		return nil, fmt.Errorf("node has 0 height")
	}

View on GitHub (pinned to 3fcf5388d7)