{"record":{"id":"d812330a89f5ef1e","repo":"grafana/k6","slug":"retrieving-cookies-w","errorCode":null,"errorMessage":"retrieving cookies: %w","messagePattern":"retrieving cookies: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":534,"sourceCode":"}\n\n// Cookies returns all cookies.\n// Some of them can be added with the AddCookies method and some of them are\n// automatically taken from the browser context when it is created. And some of\n// them are set by the page, i.e., using the Set-Cookie HTTP header or via\n// JavaScript like document.cookie.\nfunc (b *BrowserContext) Cookies(urls ...string) ([]*Cookie, error) {\n\tb.logger.Debugf(\"BrowserContext:Cookies\", \"bctxid:%v\", b.id)\n\n\t// get cookies from this browser context.\n\tgetCookies := storage.\n\t\tGetCookies().\n\t\tWithBrowserContextID(b.id)\n\tnetworkCookies, err := getCookies.Do(\n\t\tcdp.WithExecutor(b.ctx, b.browser.conn),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"retrieving cookies: %w\", err)\n\t}\n\t// return if no cookies found so we don't have to needlessly convert them.\n\t// users can still work with cookies using the empty slice.\n\t// like this: cookies.length === 0.\n\tif len(networkCookies) == 0 {\n\t\treturn nil, nil\n\t}\n\n\t// convert the received CDP cookies to the browser API format.\n\tcookies := make([]*Cookie, len(networkCookies))\n\tfor i, c := range networkCookies {\n\t\tcookies[i] = &Cookie{\n\t\t\tName:     c.Name,\n\t\t\tValue:    c.Value,\n\t\t\tDomain:   c.Domain,\n\t\t\tPath:     c.Path,\n\t\t\tExpires:  int64(c.Expires),\n\t\t\tHTTPOnly: c.HTTPOnly,","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L516-L552","documentation":"Thrown by BrowserContext.Cookies (the cookies() API) when the CDP Storage.getCookies command fails, preventing retrieval of the context's cookies. Like the other cookie endpoints, the error wraps the underlying DevTools protocol failure with the 'retrieving cookies' prefix.","triggerScenarios":"Calling browserContext.cookies([urls]) on a closed context or with a dead browser connection; Chrome failing the Storage.getCookies call for the context ID, e.g. after the context was disposed concurrently from another code path.","commonSituations":"Harvesting cookies for reuse after navigation, but doing it in a hook that runs after teardown; long-running scripts where the browser crashed mid-test (OOM) and later calls to cookies() fail; races between context.close() and final assertions.","solutions":["Ensure the context and browser are alive when calling cookies(); capture cookies before closing anything.","Check the wrapped CDP error: if it says the target/browser is gone, restart the browser rather than retrying on the dead context.","Stabilize flaky environments (raise container memory, disable unnecessary pages) if Chrome is dying during the run."],"exampleFix":"// before\nawait context.close();\nconst cookies = context.cookies(); // throws: context gone\n\n// after\nconst cookies = context.cookies();\nawait context.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const cookies = context.cookies();\n} catch (e) {\n  if (/Target closed|disconnected/.test(e.message)) {\n    // browser died mid-run: restart browser/context before retrying\n  } else {\n    throw e;\n  }\n}","preventionTips":["Harvest cookies while the context is alive (before close).","For cookie replay across contexts, read cookies immediately after the login navigation completes.","Monitor for browser crashes in long runs; a crashed browser turns every later cookie call into this error."],"tags":["cookies","cdp","browser-context","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}