grafana/k6 · error

unexpected type %T

Error message

unexpected type %T

What it means

Internal invariant in ElementHandle.fill(): the injected fill script must return a string ('done', 'needsinput', or 'error:...'), but the evaluation returned a different type. This indicates the Go/JS contract of the injected script was violated rather than a user input problem.

Solutions

  1. Upgrade k6 to the latest patch release to rule out an internal contract bug
  2. Reproduce against a minimal static page; if it only fails on your app, audit page scripts that override Object/Function/JSON
  3. Fall back to el.focus() + page.keyboard.type('...') for the same effect
  4. Report a k6 issue with a minimal reproducer if it persists on a clean page

Example fix

// workaround when fill() hits the invariant
// before
await el.fill('hello');

// after
await el.focus();
await page.keyboard.type('hello');
Defensive patterns

Strategy: fallback

Try / catch

try {
  await el.fill('hello');
} catch (e) {
  if (/unexpected type/.test(String(e))) {
    await el.focus();
    await page.keyboard.type('hello');
  } else throw e;
}

Prevention

When it happens

Trigger: el.fill('text') where evalWithScript returns a non-string because the injected script was tampered with by page JS, or an internal regression in the injected bundle; distinct from the DOM-level 'error:notfillableelement' errors which arrive as strings and are mapped by errorFromDOMError.

Common situations: Pages defining hostile prototypes or overriding globals used by the injected script; k6 version skew between generated JS helpers and the Go runtime; essentially never seen on ordinary pages.

Related errors


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

Appendix: source

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

}

func (h *ElementHandle) fill(_ context.Context, value string) error {
	fn := `
		(node, injected, value) => {
			return injected.fill(node, value);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,
	}
	result, err := h.evalWithScript(h.ctx, opts, fn, value)
	if err != nil {
		return err
	}
	s, ok := result.(string)
	if !ok {
		return fmt.Errorf("unexpected type %T", result)
	}

	if s == resultNeedsInput {
		if err := h.frame.page.Keyboard.InsertText(value); err != nil {
			return fmt.Errorf("fill: %w", err)
		}
	} else if s != resultDone {
		// Either we're done or an error happened (returned as "error:..." from JS)
		return errorFromDOMError(s)
	}

	return nil
}

func (h *ElementHandle) focus(apiCtx context.Context, resetSelectionIfNotFocused bool) error {
	fn := `
		(node, injected, resetSelectionIfNotFocused) => {
			return injected.focusNode(node, resetSelectionIfNotFocused);

View on GitHub (pinned to 3fcf5388d7)