grafana/k6 · error

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

Error message

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

What it means

Thrown by the k6 browser module's full-page screenshot path. fullPageSize() evaluates JavaScript in the page to compute max(scrollWidth/offsetWidth/clientWidth, ...Height) and then converts the result into a Go Size struct via a JSON marshal/unmarshal round-trip (helpers.go convert). If the value returned by the in-page evaluation does not deserialize into {width,height} numeric fields, this error wraps the json error and reports the offending value and its Go type.

Source

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

            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, *Size, error) {
	originalViewportSize := p.viewportSize()
	viewportSize := originalViewportSize
	if viewportSize.Width != 0 || viewportSize.Height != 0 {
		return &viewportSize, &originalViewportSize, nil
	}

	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,
	}
	result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, `
	() => (

View on GitHub (pinned to 93accf6570)

Solutions

  1. Wait for the DOM before shooting: await page.waitForLoadState('load') (or 'networkidle') and/or await page.waitForSelector('body') before page.screenshot({fullPage:true}).
  2. If the target is not an HTML document (JSON/XML/plain text response), drop fullPage:true — a viewport screenshot does not need document metrics.
  3. Wrap the call in try/catch and fall back to a non-fullPage screenshot so the test continues.
  4. Upgrade k6 — the browser module's screenshot/eval handling has had fixes around result conversion; check the k6 release notes and GitHub issues for your version.

Example fix

// before
await page.goto(url);
await page.screenshot({ path: 'full.png', fullPage: true }); // may hit converting-result error on body-less pages

// after
await page.goto(url);
await page.waitForLoadState('load');
try {
  await page.screenshot({ path: 'full.png', fullPage: true });
} catch (e) {
  if (String(e).includes('converting result')) {
    await page.screenshot({ path: 'full.png' }); // viewport fallback
  } else { throw e; }
}
Defensive patterns

Strategy: try-catch

Validate before calling

await page.waitForSelector('body', { state: 'attached' });
await page.waitForLoadState('load');

Try / catch

try {
  await page.screenshot({ path: 's.png', fullPage: true });
} catch (e) {
  if (String(e).includes('converting result')) {
    await page.screenshot({ path: 's.png' }); // fallback: viewport-only capture
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling page.screenshot({fullPage: true}) (or locator/pageLocator screenshot with fullPage) when document.body or document.documentElement is missing (the evaluated JS explicitly returns null), or when the evaluation result has an unexpected shape/type (e.g. a string or non-numeric width/height), so json.Unmarshal into Size fails.

Common situations: Screenshotting immediately after page.goto() before the DOM is built (about:blank, text/plain responses, PDF/JSON payloads, redirect interstitials); pages that navigate or rewrite document.body mid-measurement; XML/SVG documents served without an HTML body; races between load and screenshot in CI.

Related errors


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