grafana/k6 · error
missing browser context %s, current context is %s
Error message
missing browser context %s, current context is %s
What it means
Returned by newPageInContext (browser.go:510) when the browser context ID passed to browserContext.newPage() does not match the browser's current non-default context. getDefaultBrowserContextOrMatchedID falls back to the default context whenever b.context is nil (the context was disposed) or its ID differs, and the caller then detects the mismatch. It means the BrowserContext object you called newPage() on is stale — closed, or no longer the context the Browser is tracking (k6's Browser keeps a single b.context slot besides the default one).
Source
Thrown at internal/js/modules/k6/browser/common/browser.go:510
// We don't track targets of type "browser", "other" and "devtools",
// so ignore if we don't recognize target.
return
}
b.pagesMu.Lock()
defer b.pagesMu.Unlock()
if t, ok := b.pages[targetID]; ok {
b.logger.Debugf("Browser:onDetachedFromTarget:deletePage", "sid:%v tid:%v", ev.SessionID, targetID)
delete(b.pages, targetID)
t.didClose()
}
}
func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) {
bc := b.getDefaultBrowserContextOrMatchedID(id)
if bc.id != id {
return nil, fmt.Errorf("missing browser context %s, current context is %s", id, bc.id)
}
ctx, cancel := context.WithTimeout(b.vuCtx, b.browserOpts.Timeout)
defer cancel()
// buffer of one is for sending the target ID whether an event handler
// exists or not.
targetID := make(chan target.ID, 1)
waitForPage, removeEventHandler := createWaitForEventHandler(
ctx,
bc, // browser context will emit the following event:
[]string{EventBrowserContextPage},
func(e any) bool {
tid := <-targetID
b.logger.Debugf("Browser:newPageInContext:createWaitForEventHandler",
"tid:%v ptid:%v bctxid:%v", tid, e.(*Page).targetID, id) //nolint:forcetypeassertView on GitHub (pinned to 93accf6570)
Solutions
- Create a fresh context with browser.newContext() instead of reusing the closed/stale one
- Call newPage() only while the context is open — before close()
- Audit code for cached context references; pass pages, not contexts, to helpers
- Restructure to a single newContext()…close() block per test instead of interleaving multiple contexts
Example fix
// before const ctx = browser.newContext() const p1 = ctx.newPage() await ctx.close() const p2 = ctx.newPage() // throws: missing browser context // after const ctx = browser.newContext() const p1 = ctx.newPage() await ctx.close() const ctx2 = browser.newContext() // fresh context const p2 = ctx2.newPage()
Defensive patterns
Strategy: validation
Validate before calling
function withContext(browser, fn) {
const ctx = browser.newContext()
try {
return fn(ctx)
} finally {
ctx.close()
}
}
// always create pages inside the context's live window Try / catch
try {
const page = ctx.newPage()
} catch (e) {
if (/missing browser context/.test(String(e))) {
ctx = browser.newContext() // stale context — recreate and continue
page = ctx.newPage()
} else throw e
} Prevention
- Never call newPage() after close(); null context references on close
- Keep exactly one newContext()…close() lifecycle per test
- Pass pages, not contexts, to helper functions
When it happens
Trigger: Calling newPage() after context.close() (disposeContext clears b.context, the lookup falls back to the default context and IDs differ); holding a reference to an older context after browser.newContext() created a newer one, then calling newPage() on the old reference.
Common situations: Reusing a context across setup/test/teardown after it was closed; helper functions caching the result of newContext(); porting Playwright tests that juggle several live contexts — the k6 browser module tracks only one non-default context at a time.
Related errors
- parsing browser.newPage options: %w
- default browser context can't be closed
- disposing browser context: %w
- clearing cookies: %w
- retrieving cookies: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/0fde2bbc9d97d562.
Report an issue: GitHub.