grafana/k6 · error

converting result ( of type %t) to size

Error message

converting result (%v of type %t) to size: %w

What it means

Thrown during full-page screenshots when the JS expression computing document.body/documentElement scroll/offset/client dimensions evaluates successfully, but its result cannot be converted into a Size struct. The message helpfully embeds both the result value and its Go type, so you can see exactly what the page returned instead of {width, height}.

Solutions

  1. Inspect the (%v of type %t) part of the message — it shows the actual value returned; 'null' means the measurement raced a DOM change.
  2. Take the full-page screenshot after the page settles: waitForLoadState or an explicit selector wait first.
  3. Retry the screenshot once — this is a race between evaluation and DOM mutation.
  4. If it persists, screenshot without fullPage (viewport-only) or against a stabilized DOM.

Example fix

// before
await page.screenshot({ fullPage: true }); // during DOM churn

// after
await page.waitForLoadState('networkidle');
await page.screenshot({ fullPage: true });
Defensive patterns

Strategy: retry

Validate before calling

await page.waitForLoadState('networkidle'); // let DOM settle before fullPage capture

Try / catch

try {
  await page.screenshot({ fullPage: true, path: 'f.png' });
} catch (e) {
  if (/converting result .* to size/i.test(String(e))) {
    await page.waitForTimeout(500);
    await page.screenshot({ fullPage: true, path: 'f.png' });
  } else throw e;
}

Prevention

When it happens

Trigger: page.screenshot({ fullPage: true }) where document.documentElement/HTML structure is unusual (e.g. height evaluates to null/undefined so the whole object serializes partially, or the page replaced the document during measurement). Fast DOM mutations between evaluation and conversion, or a page whose frames return a non-object, trigger it.

Common situations: Full-page screenshots of SPAs that rewrite the DOM continuously (infinite scroll, virtualized lists); pages inside iframe-heavy apps during teardown; screenshots taken while a navigation is starting.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/27500b0bb354a3c7. Report an issue: GitHub.

Appendix: source

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

            return {
                width: Math.max(
                    document.body.scrollWidth, document.documentElement.scrollWidth,
                    document.body.offsetWidth, document.documentElement.offsetWidth,
                    document.body.clientWidth, document.documentElement.clientWidth
                ),
                height: Math.max(
                    document.body.scrollHeight, document.documentElement.scrollHeight,
                    document.body.offsetHeight, document.documentElement.offsetHeight,
                    document.body.clientHeight, document.documentElement.clientHeight
                ),
            };
        }`)
	if err != nil {
		return nil, err
	}
	var size Size
	if err := convert(result, &size); err != nil {
		return nil, fmt.Errorf("converting result (%v of type %t) to size: %w", result, result, err)
	}

	return &size, nil
}

func (s *screenshotter) originalViewportSize(p *Page) (*Size, error) {
	viewportSize := p.viewportSize()
	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 }

View on GitHub (pinned to 3fcf5388d7)