{"record":{"id":"cf9c1286b112ea59","repo":"grafana/k6","slug":"setting-offline-status-to-t-for-the-browser-conte","errorCode":null,"errorMessage":"setting offline status to %t for the browser context ID %s: %w","messagePattern":"setting offline status to %t for the browser context ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":336,"sourceCode":"\n\tb.opts.HTTPCredentials = hc\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateHTTPCredentials(); err != nil {\n\t\t\treturn fmt.Errorf(\"setting HTTP credentials in target ID %s: %w\", p.targetID, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// SetOffline toggles the browser's connectivity on/off.\nfunc (b *BrowserContext) SetOffline(offline bool) error {\n\tb.logger.Debugf(\"BrowserContext:SetOffline\", \"bctxid:%v offline:%t\", b.id, offline)\n\n\tb.opts.Offline = offline\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateOffline(); err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"setting offline status to %t for the browser context ID %s: %w\",\n\t\t\t\toffline, b.id, err,\n\t\t\t)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// Timeout will return the default timeout or the one set by the user.\nfunc (b *BrowserContext) Timeout() time.Duration {\n\treturn b.timeoutSettings.timeout()\n}\n\n// WaitForEvent waits for event.\nfunc (b *BrowserContext) WaitForEvent(event string, f func(p *Page) (bool, error), timeout time.Duration) (any, error) {\n\tb.logger.Debugf(\"BrowserContext:WaitForEvent\", \"bctxid:%v event:%q\", b.id, event)\n","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L318-L354","documentation":"Thrown by BrowserContext.SetOffline when toggling offline mode fails for one of the open pages. The method stores the flag in the context options, then calls updateOffline on every page, which sends a Network emulation CDP command to each frame session; a failing CDP call is wrapped with the requested offline value and the browser context ID.","triggerScenarios":"Calling browserContext.setOffline(true) or setOffline(false) when the CDP connection to a page's frame session is dead: browser crashed or is shutting down, target was closed concurrently, or the websocket was dropped. The offline flag itself is validated only by whether the underlying emulation command succeeds.","commonSituations":"Offline-mode tests that call setOffline around navigation steps and race with page.close() or test teardown; running headless Chrome in resource-starapped containers where the target process dies; calling setOffline after the browser context has already been disposed.","solutions":["Make sure the browser context and all its pages are still open when calling setOffline (don't call it after page.close() or during teardown).","Check k6 debug logs for the underlying CDP error to see whether the browser target died ('Target closed', 'websocket disconnected') and restart the browser if so.","If the failure is a race with closing pages, restructure the script to toggle offline before opening pages or after all pages you need are confirmed open."],"exampleFix":"// before\npage.close();\ncontext.setOffline(true); // page target already gone\n\n// after\ncontext.setOffline(true);\n// ... run offline assertions ...\ncontext.setOffline(false);\npage.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  context.setOffline(true);\n} catch (e) {\n  if (/Target closed|disconnected/.test(e.message)) {\n    // browser/page gone: recreate context instead of retrying\n  } else {\n    throw e;\n  }\n}","preventionTips":["Toggle offline only while the context and all pages are open.","Order teardown so offline toggles happen before close() calls.","Log the wrapped CDP error to distinguish lifecycle failures from emulation failures."],"tags":["network","offline","cdp","browser-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}