grafana/k6 · error

adding script to evaluate on new document: %w

Error message

adding script to evaluate on new document: %w

What it means

At the end of initIsolatedWorld (frame_session.go:496), k6 registers its evaluation helper script via Page.addScriptToEvaluateOnNewDocument with the isolated world name. If the CDP call fails, world initialization fails with this wrapped error. Like the other init commands, failure signals a dead/closing target rather than a problem with your selector or script.

Source

Thrown at internal/js/modules/k6/browser/common/frame_session.go:496

		// A frame could have been removed before we execute this, so don't wait around for a reply.
		_ = fs.session.ExecuteWithoutExpectationOnReply(
			fs.ctx,
			cdppage.CommandCreateIsolatedWorld,
			&cdppage.CreateIsolatedWorldParams{
				FrameID:             cdp.FrameID(frame.ID()),
				WorldName:           name,
				GrantUniveralAccess: true,
			},
			nil)
	}

	fs.logger.Debugf("NewFrameSession:initIsolatedWorld:AddScriptToEvaluateOnNewDocument",
		"sid:%v tid:%v", fs.session.ID(), fs.targetID)

	action2 := cdppage.AddScriptToEvaluateOnNewDocument(`//# sourceURL=` + evaluationScriptURL).
		WithWorldName(name)
	if _, err := action2.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
		return fmt.Errorf("adding script to evaluate on new document: %w", err)
	}
	return nil
}

func (fs *FrameSession) initOptions() error {
	fs.logger.Debugf("NewFrameSession:initOptions",
		"sid:%v tid:%v", fs.session.ID(), fs.targetID)

	var (
		opts       = fs.manager.page.browserCtx.opts
		optActions = []Action{}
		state      = fs.vu.State()
	)

	if fs.isMainFrame() {
		optActions = append(optActions, emulation.SetFocusEmulationEnabled(true))
		if err := fs.updateViewport(); err != nil {
			fs.logger.Debugf("NewFrameSession:initOptions:updateViewport",

View on GitHub (pinned to 93accf6570)

Solutions

  1. Re-issue the wait/goto after the page settles — a fresh navigation re-runs init against a live target
  2. Keep browser lifetimes bounded: create contexts/pages per iteration instead of one long-lived browser for very long tests
  3. Check chromium logs for renderer crashes if the error repeats on every attempt
Defensive patterns

Strategy: retry

Try / catch

try {
  frame.waitForSelector(sel);
} catch (e) {
  if (/adding script to evaluate on new document/i.test(String(e))) {
    // world setup raced navigation — navigate again, then retry the wait
  }
}

Prevention

When it happens

Trigger: Isolated-world setup racing page navigation or close: the world is created lazily on first waitFor* usage, so a target that becomes invalid between creation and this call fails here. Renderer crash produces the same result.

Common situations: Sites that immediately navigate/redirect after load, popups closed by the app, and long-running suites where chromium has degraded (memory leaks, zombie renderers).

Related errors


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