grafana/k6 · error
adding k6 object to new browser context
Error message
adding k6 object to new browser context: %w
What it means
Wrapper in initBrowserContext around AddInitScript failing to inject the window.k6 = { testRunId } metadata script into the new browser context. The wrapped CDP error is the real cause — typically the target/page was already closed or the CDP session broke during context setup.
Solutions
- Check the wrapped error for the CDP failure reason
- Verify the browser binary is compatible (matching Chromium version)
- Retry the operation — transient CDP disconnects during context creation can cause this
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at internal/js/modules/k6/browser/browser/browser_mapping.go:144 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/a046ef267d34c2f5.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/browser/browser_mapping.go:144
}
m["close"] = func() *sobek.Promise {
return promise(vu, func() (any, error) {
vu.untrackUserManagedBrowser(iter, b)
b.Close()
return nil, nil
})
}
}
func initBrowserContext(bctx *common.BrowserContext, testRunID string) error {
// Setting a k6 object which will contain k6 specific metadata
// on the current test run. This allows external applications
// (such as Grafana Faro) to identify that the session is a k6
// automated one and not one driven by a real person.
if err := bctx.AddInitScript(
fmt.Sprintf(`window.k6 = { testRunId: %q }`, testRunID),
); err != nil {
return fmt.Errorf("adding k6 object to new browser context: %w", err)
}
return nil
}
// parseBrowserContextOptions parses the [common.BrowserContext] options from a Sobek value.
func parseBrowserContextOptions(rt *sobek.Runtime, opts sobek.Value) (*common.BrowserContextOptions, error) {
b := common.DefaultBrowserContextOptions()
if err := mergeWith(rt, b, opts); err != nil {
return nil, err
}
if err := b.Proxy.Validate(); err != nil {
return nil, err
}
return b, nil
}
View on GitHub (pinned to 01ffac6f24)