grafana/k6 · error

fill

Error message

fill: %w

What it means

fill() asked the injected script to prepare the element and got back 'needsinput', meaning the text must be inserted via the keyboard (contenteditable and special input cases). The error wraps a failure of Keyboard.InsertText — the CDP Input.insertText call — while typing the value.

Solutions

  1. Wait for the editor to be fully loaded and focused before filling: await el.waitFor({ state: 'visible' }); await el.click();
  2. Retry the fill once — transient session hiccups and remounts often clear
  3. For contenteditable, prefer focus + page.keyboard.insertText(value) or type() so you control pacing
  4. Check for browser crashes/page closure in the surrounding log; raise --timeout or the action timeout if the fill is racing a redirect

Example fix

// before
await page.locator('[contenteditable]').fill('draft');

// after
const ed = page.locator('[contenteditable]');
await ed.waitFor({ state: 'visible' });
await ed.click();
await page.keyboard.insertText('draft');
Defensive patterns

Strategy: retry

Validate before calling

// ensure the editable area is focused and stable before fill
await el.waitFor({ state: 'visible' });
await el.click();

Try / catch

try {
  await el.fill(value);
} catch (e) {
  if (/fill:/.test(String(e)) && !page.isClosed()) {
    await page.waitForTimeout(500);
    await el.fill(value); // single retry
  } else throw e;
}

Prevention

When it happens

Trigger: el.fill(value) on a contenteditable element or special input where the page navigates, closes, or crashes between the fill preparation and InsertText; the browser session/CDP target dies mid-call (browser crashed, page closed by the app, target swapped).

Common situations: Filling rich-text editors (contenteditable) in SPAs that remount or redirect on input; the page's own JS closing/replacing the node mid-fill; resource-exhausted CI browsers crashing; timeouts firing mid-fill.

Related errors


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

Appendix: source

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

			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);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: true,

View on GitHub (pinned to 3fcf5388d7)