{"record":{"id":"f508e2fb0ffd50d2","repo":"grafana/k6","slug":"evaluating-script-on-document-w","errorCode":null,"errorMessage":"evaluating script on document: %w","messagePattern":"evaluating script on document: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":637,"sourceCode":"}\n\nfunc (p *Page) didClose() {\n\tp.logger.Debugf(\"Page:didClose\", \"sid:%v\", p.sessionID())\n\n\tp.closedMu.Lock()\n\t{\n\t\tp.closed = true\n\t}\n\tp.closedMu.Unlock()\n}\n\nfunc (p *Page) evaluateOnNewDocument(source string) error {\n\tp.logger.Debugf(\"Page:evaluateOnNewDocument\", \"sid:%v\", p.sessionID())\n\n\taction := page.AddScriptToEvaluateOnNewDocument(source)\n\t_, err := action.Do(cdp.WithExecutor(p.ctx, p.session))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"evaluating script on document: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (p *Page) getFrameElement(f *Frame) (handle *ElementHandle, _ error) {\n\tif f == nil {\n\t\tp.logger.Debugf(\"Page:getFrameElement\", \"sid:%v frame:nil\", p.sessionID())\n\t} else {\n\t\tp.logger.Debugf(\"Page:getFrameElement\", \"sid:%v fid:%s furl:%s\",\n\t\t\tp.sessionID(), f.ID(), f.URL())\n\t}\n\n\tparent := f.parentFrame\n\tif parent == nil {\n\t\treturn nil, errors.New(\"frame has been detached 1\")\n\t}\n","sourceCodeStart":619,"sourceCodeEnd":655,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/page.go#L619-L655","documentation":"Page.evaluateOnNewDocument failed to register the script with CDP: the Page.addScriptToEvaluateOnNewDocument command returned an error. This runs when the browser module (or a context's addInitScript path) tries to evaluate a script on every new document, and the page's session could not execute the command.","triggerScenarios":"Registering an init/evaluate-on-new-document script while the page session is closed or closing — e.g. addInitScript after page.close() began, or during teardown races at iteration end; or on a dead session after a browser crash.","commonSituations":"Late addInitScript calls racing context/page close; scripts injecting instrumentation right before navigation completes on a swapped target; running after an earlier crash made the session unusable.","solutions":["Call addInitScript before any goto()/newPage-dependent work, ideally right after context creation","Await it so failures surface immediately rather than racing navigation","If it appears at iteration end only, restructure to finish script setup before teardown starts","Check for upstream session failures (DEBUG=k6-browser) when it appears mid-test"],"exampleFix":"// before\nconst page = await browser.newPage();\nawait page.goto('https://test.k6.io/');\nawait page.addInitScript ? null : null; // late registration path\n\n// after\nconst ctx = browser.newContext();\nctx.addInitScript('window.marker = true;'); // registered on live session\nconst page = await ctx.newPage();\nawait page.goto('https://test.k6.io/');","handlingStrategy":"validation","validationCode":"if (page.isClosed()) { throw new Error('page closed; init script registration will fail'); }\nawait ctx.addInitScript('window.marker = 1;');","typeGuard":null,"tryCatchPattern":"try {\n  await ctx.addInitScript(src);\n} catch (e) {\n  if (/evaluating script on document/.test(String(e))) return; // session gone; skip\n  throw e;\n}","preventionTips":["Register init scripts before pages exist or before goto()","Await addInitScript so failures surface immediately","Keep init scripts small, valid JS — they run on every new document"],"tags":["cdp","browser","init-script","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}