grafana/k6 · error

checking element is in viewport: unexpected type %T

Error message

checking element is in viewport: unexpected type %T

What it means

isInViewport() evaluates injected.isInViewport, which must return a boolean or an 'error:...' string. Any other Go type from the evaluation triggers this error, indicating the injected-script contract broke or the context died mid-eval.

Source

Thrown at internal/js/modules/k6/browser/common/element_handle.go:417

			return injected.isInViewport(node, ratio);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,
	}
	result, err := h.evalWithScript(apiCtx, opts, fn, ratio)
	if err != nil {
		return false, errorFromDOMError(err)
	}
	switch v := result.(type) {
	case string: // An error happened (returned as "error:..." from JS)
		return false, errorFromDOMError(v)
	case bool:
		return v, nil
	}

	return false, fmt.Errorf(
		"checking element is in viewport: unexpected type %T", result)
}

func (h *ElementHandle) offsetPosition(apiCtx context.Context, offset *Position) (*Position, error) {
	fn := `
		(node, injected) => {
			return injected.getElementBorderWidth(node);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,
	}
	result, err := h.evalWithScript(apiCtx, opts, fn)
	if err != nil {
		return nil, err
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Retry after the page settles
  2. Upgrade to a clean, matching k6 version
  3. Avoid racing navigation: wait for load state before asserting viewport visibility
  4. File an issue with a reproduction if it recurs on a stock build
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const visible = await handle.isVisible(); // related state checks
} catch (e) {
  if (String(e).includes('unexpected type')) { /* retry with fresh handle */ } 
  else { throw e; }
}

Prevention

When it happens

Trigger: Viewport-membership checks (used by actionability logic and internal waits) when the eval returns undefined or an object — execution context destroyed during evaluation or an injected-script/binary version mismatch.

Common situations: Navigation or iframe teardown racing the check; custom builds with stale injected script; very rare on stock releases.

Related errors


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