grafana/k6 · error

unpacking window size

Error message

unpacking window size: %w

What it means

Companion to the previous error: the window.innerWidth/innerHeight evaluation succeeded, but converting the result into a Size struct failed ('unpacking window size'). The evaluate runs with returnByValue: true, so a non-object or partially-shaped object (missing width/height, or non-numeric values) is the cause.

Solutions

  1. Take the screenshot after real content has loaded (wait for a selector or 'load').
  2. Prefer launching with an explicit viewport so the fallback evaluation is never used.
  3. If it reproduces on about:blank, navigate to the target page first.

Example fix

// before
const page = await context.newPage(); // viewport: null context
await page.screenshot({ path: 'p.png' }); // too early

// after
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'p.png' });
Defensive patterns

Strategy: validation

Validate before calling

// ensure real content exists before capturing in viewport:null mode
await page.goto(url);
await page.waitForSelector('body *');

Prevention

When it happens

Trigger: Screenshotting in viewport:null mode on a page whose window object reports undefined dimensions (e.g. during document replacement, or in about:blank edge cases right after creation), so the returned value lacks usable width/height fields.

Common situations: Screenshots immediately after browser.newPage() before any content loads, in real-viewport mode; exotic embedded pages where innerWidth is not yet a number.

Related errors


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

Appendix: source

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

	if viewportSize.Width != 0 || viewportSize.Height != 0 {
		return &viewportSize, nil
	}

	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,
	}
	result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, `
	() => (
		{ width: window.innerWidth, height: window.innerHeight }
	)`)
	if err != nil {
		return nil, fmt.Errorf("getting viewport dimensions: %w", err)
	}

	var returnVal Size
	if err := convert(result, &returnVal); err != nil {
		return nil, fmt.Errorf("unpacking window size: %w", err)
	}

	viewportSize.Width = returnVal.Width
	viewportSize.Height = returnVal.Height

	return &viewportSize, nil
}

func (s *screenshotter) restoreViewport(p *Page, originalSize *EmulatedSize) error {
	ctx, cancel := s.cleanupContext()
	defer cancel()
	if originalSize != nil {
		return p.setEmulatedSize(ctx, originalSize)
	}
	return p.resetViewport(ctx)
}

func (s *screenshotter) screenshot(

View on GitHub (pinned to 3fcf5388d7)