grafana/k6 · error

getting injected script: %w

Error message

getting injected script: %w

What it means

Inside waitForFunction, k6 evaluates the injected-script expression (injectedScript.js utility) in the frame's execution world; failure to evaluate/serialize it is wrapped as 'getting injected script: <cause>'. This is an internal prerequisite step — the user's predicate has not run yet — and fails when the context is unusable or the injected script source cannot be fetched.

Source

Thrown at internal/js/modules/k6/browser/common/frame.go:1991

	polling any, timeout time.Duration, args ...any,
) (any, error) {
	f.log.Debugf(
		"Frame:waitForFunction",
		"fid:%s furl:%q world:%s poll:%s timeout:%s",
		f.ID(), f.URL(), world, polling, timeout)

	f.waitForExecutionContext(world)

	f.executionContextMu.RLock()
	defer f.executionContextMu.RUnlock()

	execCtx := f.executionContexts[world]
	if execCtx == nil {
		return nil, fmt.Errorf("waiting for function: execution context %q not found", world)
	}
	injected, err := execCtx.getInjectedScript(apiCtx)
	if err != nil {
		return nil, fmt.Errorf("getting injected script: %w", err)
	}

	pageFn := `
		(injected, predicate, polling, timeout, ...args) => {
			return injected.waitForPredicateFunction(predicate, polling, timeout, ...args);
		}
	`

	// First evaluate the predicate function itself to get its handle.
	opts := evalOptions{forceCallable: false, returnByValue: false}
	handle, err := execCtx.eval(apiCtx, opts, js)
	if err != nil {
		return nil, fmt.Errorf("waiting for function, getting handle: %w", err)
	}

	// Then evaluate the injected function call, passing it the predicate
	// function handle and the rest of the arguments.
	opts = evalOptions{forceCallable: true, returnByValue: false}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Retry the waitForFunction once after waiting for a stable load state — transient context destruction self-heals after navigation completes.
  2. Start polling only after domcontentloaded/load to avoid the navigation window.
  3. If recurring, capture DEBUG=br* logs — an injected-script fetch failure is a k6 issue worth reporting with the log.

Example fix

// before
await frame.waitForFunction(fn);

// after
await frame.waitForLoadState('domcontentloaded');
await frame.waitForFunction(fn).catch(async (e) => {
  await frame.waitForLoadState('load');
  return frame.waitForFunction(fn);
});
Defensive patterns

Strategy: retry

Validate before calling

await frame.waitForLoadState('domcontentloaded');

Try / catch

try { return await frame.waitForFunction(fn, arg, { timeout }); }
catch (e) {
  if (/getting injected script/.test(e.message)) {
    await frame.waitForLoadState('load');
    return frame.waitForFunction(fn, arg, { timeout });
  }
  throw e;
}

Prevention

When it happens

Trigger: The execution context found earlier is destroyed between the lookup and the eval (navigation race); Page.addScriptTag/evaluate of the utility fails because the renderer is gone; CDP call erroring with context destroyed. Immediately follows error 817's conditions when they develop mid-call.

Common situations: Fast-navigating SPAs during waitForFunction polling; iframes torn down while a poll is starting; browser under memory pressure crashing renderers.

Related errors


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