{"record":{"id":"9b9723ec22f3f411","repo":"grafana/k6","slug":"adding-script-to-evaluate-on-new-document-w","errorCode":null,"errorMessage":"adding script to evaluate on new document: %w","messagePattern":"adding script to evaluate on new document: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame_session.go","lineNumber":496,"sourceCode":"\t\t// A frame could have been removed before we execute this, so don't wait around for a reply.\n\t\t_ = fs.session.ExecuteWithoutExpectationOnReply(\n\t\t\tfs.ctx,\n\t\t\tcdppage.CommandCreateIsolatedWorld,\n\t\t\t&cdppage.CreateIsolatedWorldParams{\n\t\t\t\tFrameID:             cdp.FrameID(frame.ID()),\n\t\t\t\tWorldName:           name,\n\t\t\t\tGrantUniveralAccess: true,\n\t\t\t},\n\t\t\tnil)\n\t}\n\n\tfs.logger.Debugf(\"NewFrameSession:initIsolatedWorld:AddScriptToEvaluateOnNewDocument\",\n\t\t\"sid:%v tid:%v\", fs.session.ID(), fs.targetID)\n\n\taction2 := cdppage.AddScriptToEvaluateOnNewDocument(`//# sourceURL=` + evaluationScriptURL).\n\t\tWithWorldName(name)\n\tif _, err := action2.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {\n\t\treturn fmt.Errorf(\"adding script to evaluate on new document: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (fs *FrameSession) initOptions() error {\n\tfs.logger.Debugf(\"NewFrameSession:initOptions\",\n\t\t\"sid:%v tid:%v\", fs.session.ID(), fs.targetID)\n\n\tvar (\n\t\topts       = fs.manager.page.browserCtx.opts\n\t\toptActions = []Action{}\n\t\tstate      = fs.vu.State()\n\t)\n\n\tif fs.isMainFrame() {\n\t\toptActions = append(optActions, emulation.SetFocusEmulationEnabled(true))\n\t\tif err := fs.updateViewport(); err != nil {\n\t\t\tfs.logger.Debugf(\"NewFrameSession:initOptions:updateViewport\",","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame_session.go#L478-L514","documentation":"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.","triggerScenarios":"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.","commonSituations":"Sites that immediately navigate/redirect after load, popups closed by the app, and long-running suites where chromium has degraded (memory leaks, zombie renderers).","solutions":["Re-issue the wait/goto after the page settles — a fresh navigation re-runs init against a live target","Keep browser lifetimes bounded: create contexts/pages per iteration instead of one long-lived browser for very long tests","Check chromium logs for renderer crashes if the error repeats on every attempt"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  frame.waitForSelector(sel);\n} catch (e) {\n  if (/adding script to evaluate on new document/i.test(String(e))) {\n    // world setup raced navigation — navigate again, then retry the wait\n  }\n}","preventionTips":["Scope browser/context lifetimes to iterations in long tests","Check chromium logs for renderer crashes when this repeats"],"tags":["browser","cdp","isolated-world","evaluation","transient"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}