grafana/k6 · error
adding web vital script to new browser context: %w
Error message
adding web vital script to new browser context: %w
What it means
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.
Source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:151
b := BrowserContext{
BaseEventEmitter: NewBaseEventEmitter(ctx),
ctx: ctx,
browser: browser,
id: id,
opts: opts,
logger: logger,
timeoutSettings: NewTimeoutSettings(nil),
}
if len(opts.Permissions) > 0 {
err := b.GrantPermissions(opts.Permissions, GrantPermissionsOptions{})
if err != nil {
return nil, err
}
}
if err := b.AddInitScript(js.WebVitalIIFEScript); err != nil {
return nil, fmt.Errorf("adding web vital script to new browser context: %w", err)
}
if err := b.AddInitScript(js.WebVitalInitScript); err != nil {
return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err)
}
if err := b.setDownloadsPath(opts.DownloadsPath); err != nil {
return nil, fmt.Errorf("setting downloads path: %w", err)
}
return &b, nil
}
// AddInitScript adds a script that will be initialized on all new pages.
func (b *BrowserContext) AddInitScript(script string) error {
b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id)
b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script)
for _, p := range b.browser.getPages() {View on GitHub (pinned to 93accf6570)
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
Defensive patterns
Strategy: retry
Validate before calling
// create all contexts early in the iteration, before load/navigation // const ctx = browser.newContext() — do it right after launch
Try / catch
try {
ctx = browser.newContext()
} catch (e) {
if (/adding web vital script/.test(String(e)) && /connection|closed/i.test(String(e))) {
throw e // chromium died — restarting requires a new browser
}
throw e
} Prevention
- 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
When it happens
Trigger: browser.newContext() issued while the CDP connection is dropping or chromium just crashed; an existing page's session already detached; browser.close() racing newContext().
Common situations: Under-resourced chromium dying mid-test (OOM, no shm); calling newContext() after the browser disconnected; creating contexts late in the iteration near teardown.
Related errors
- adding web vital init script to new browser context: %w
- adding init script to browser context: %w
- adding k6 object to new browser context: %w
- connecting to Chromium over CDP: %w
- browser connect: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/239291e39047a66b.
Report an issue: GitHub.