{"record":{"id":"239291e39047a66b","repo":"grafana/k6","slug":"adding-web-vital-script-to-new-browser-context-w","errorCode":null,"errorMessage":"adding web vital script to new browser context: %w","messagePattern":"adding web vital script to new browser context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":151,"sourceCode":"\tb := BrowserContext{\n\t\tBaseEventEmitter: NewBaseEventEmitter(ctx),\n\t\tctx:              ctx,\n\t\tbrowser:          browser,\n\t\tid:               id,\n\t\topts:             opts,\n\t\tlogger:           logger,\n\t\ttimeoutSettings:  NewTimeoutSettings(nil),\n\t}\n\n\tif len(opts.Permissions) > 0 {\n\t\terr := b.GrantPermissions(opts.Permissions, GrantPermissionsOptions{})\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tif err := b.AddInitScript(js.WebVitalIIFEScript); err != nil {\n\t\treturn nil, fmt.Errorf(\"adding web vital script to new browser context: %w\", err)\n\t}\n\tif err := b.AddInitScript(js.WebVitalInitScript); err != nil {\n\t\treturn nil, fmt.Errorf(\"adding web vital init script to new browser context: %w\", err)\n\t}\n\tif err := b.setDownloadsPath(opts.DownloadsPath); err != nil {\n\t\treturn nil, fmt.Errorf(\"setting downloads path: %w\", err)\n\t}\n\n\treturn &b, nil\n}\n\n// AddInitScript adds a script that will be initialized on all new pages.\nfunc (b *BrowserContext) AddInitScript(script string) error {\n\tb.logger.Debugf(\"BrowserContext:AddInitScript\", \"bctxid:%v\", b.id)\n\n\tb.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script)\n\n\tfor _, p := range b.browser.getPages() {","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L133-L169","documentation":"Creating a browser context registers k6's web-vitals collection scripts on every already-existing page via CDP Page.addScriptToEvaluateOnNewDocument (browser_context.go:151). This is the first of two injections (the WebVitalIIFEScript bootstrap); if the CDP round-trip fails on any existing page, NewBrowserContext aborts with this wrap.","triggerScenarios":"browser.newContext() issued while the CDP connection is dropping or chromium just crashed; an existing page's session already detached; browser.close() racing newContext().","commonSituations":"Under-resourced chromium dying mid-test (OOM, no shm); calling newContext() after the browser disconnected; creating contexts late in the iteration near teardown.","solutions":["Read the wrapped CDP error — connection errors mean chromium died; confirm with K6_BROWSER_LOG=debug","Create all contexts early, before heavy navigation/load","Add resources (memory, --shm-size) or reduce VUs if chromium is OOM-killed","Upgrade k6; init-script injection and crash handling improved across releases"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// create all contexts early in the iteration, before load/navigation\n// const ctx = browser.newContext() — do it right after launch","typeGuard":null,"tryCatchPattern":"try {\n  ctx = browser.newContext()\n} catch (e) {\n  if (/adding web vital script/.test(String(e)) && /connection|closed/i.test(String(e))) {\n    throw e // chromium died — restarting requires a new browser\n  }\n  throw e\n}","preventionTips":["Create contexts immediately after launch, when no pages exist","Keep chromium resourced (memory/shm) so it survives the whole test","Check K6_BROWSER_LOG=debug when context creation fails intermittently"],"tags":["cdp","init-script","web-vitals","browser-context","connection"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}