{"record":{"id":"220e439eb08f81f3","repo":"grafana/k6","slug":"internal-error-while-applying-init-scripts-to-page","errorCode":null,"errorMessage":"internal error while applying init scripts to page: %w","messagePattern":"internal error while applying init scripts to page: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":365,"sourceCode":"\tp.frameSessions[cdp.FrameID(tid)] = p.mainFrameSession\n\tp.frameSessionsMu.Unlock()\n\tp.Mouse = NewMouse(ctx, s, p.frameManager.MainFrame(), bctx.timeoutSettings, p.Keyboard)\n\tp.Touchscreen = NewTouchscreen(ctx, s, p.Keyboard)\n\n\tp.initEvents()\n\n\taction := target.SetAutoAttach(true, true).WithFlatten(true)\n\tif err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while auto attaching to browser pages: %w\", err)\n\t}\n\n\tadd := runtime.AddBinding(webVitalBinding)\n\tif err := add.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while adding binding to page: %w\", err)\n\t}\n\n\tif err := bctx.applyAllInitScripts(&p); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while applying init scripts to page: %w\", err)\n\t}\n\n\treturn &p, nil\n}\n\nfunc (p *Page) initEvents() {\n\tp.logger.Debugf(\"Page:initEvents\",\n\t\t\"sid:%v tid:%v\", p.session.ID(), p.targetID)\n\n\tevents := []string{\n\t\tcdproto.EventRuntimeConsoleAPICalled,\n\t}\n\tp.session.on(p.ctx, events, p.eventCh)\n\n\tgo func() {\n\t\tp.logger.Debugf(\"Page:initEvents:go\",\n\t\t\t\"sid:%v tid:%v\", p.session.ID(), p.targetID)\n\t\tdefer func() {","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L347-L383","documentation":"Failure of the CDP page.addScriptToEvaluateOnNewDocument command while installing the init scripts (web vitals collection, context cookie bridge, etc.) during page creation. The offending input is the injected script source; failure usually indicates a dead/rejected session during page setup rather than script content.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/page.go:363 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Retry browser/page creation on transient CDP failures","Check custom init scripts (addInitScript) for extremely large or invalid payloads","Report as a bug if it occurs consistently with a working browser"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}