grafana/k6 · error
if cookie URL is not provided, both domain and path must be…
Error message
if cookie URL is not provided, both domain and path must be specified: %#v
What it means
Client-side validation in AddCookies mirroring CDP's Network.setCookie rules: if a cookie has no URL, BOTH Domain and Path must be set. A cookie with only a domain, only a path, or neither is rejected with the full cookie printed via %#v. No CDP call is made.
Solutions
- Provide a url per cookie — the simplest valid form: { name, value, url: 'https://example.com/path' }
- Or provide both domain and path explicitly: { name, value, domain: 'example.com', path: '/' }
- When copying cookies from ctx.cookies(), carry over domain and path unchanged instead of rebuilding objects
Example fix
// before
ctx.addCookies([{ name: 'session', value: 'abc' }]); // no url, no domain/path
// after
ctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);
// or: { name: 'session', value: 'abc', domain: 'example.com', path: '/' } Defensive patterns
Strategy: validation
Validate before calling
function isValidCookieScoping(c) {
return (typeof c.url === 'string' && c.url !== '')
|| ((typeof c.domain === 'string' && c.domain !== '')
&& (typeof c.path === 'string' && c.path !== ''));
}
const cookies = [{ name: 'session', value: 'abc' }].map((c) => ({
...c,
url: 'https://example.com', // ensure scoping is always present
}));
if (cookies.every(isValidCookieScoping)) ctx.addCookies(cookies); Type guard
const isScopedCookie = (c) =>
c != null && typeof c === 'object'
&& ((typeof c.url === 'string' && c.url) !== ''
|| (Boolean(c.domain) && Boolean(c.path))); Prevention
- Default to giving each cookie a url — simplest valid scoping
- Otherwise always set both domain and path (path: '/' is typical)
- When copying cookies via ctx.cookies(), keep their domain/path fields intact
When it happens
Trigger: browserContext.addCookies([{ name: 'n', value: 'v' }]) with neither url nor domain/path; cookies parsed from document.cookie strings (which carry no domain/path attributes); only domain: 'example.com' provided without path.
Common situations: Porting cookies captured from document.cookie or from another context's cookies() output and dropping fields; assuming the browser defaults path to '/' — k6 requires you to be explicit.
Related errors
- cookie name must be set: %#v
- cookie value must be set: %#v
- no cookies provided
- cannot set cookies
- default browser context can't be closed
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/f96691bdaab6f780.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:474
b.logger.Debugf("BrowserContext:AddCookies", "bctxid:%v", b.id)
// skip work if no cookies provided.
if len(cookies) == 0 {
return fmt.Errorf("no cookies provided")
}
cookiesToSet := make([]*network.CookieParam, 0, len(cookies))
for _, c := range cookies {
if c.Name == "" {
return fmt.Errorf("cookie name must be set: %#v", c)
}
if c.Value == "" {
return fmt.Errorf("cookie value must be set: %#v", c)
}
// if URL is not set, both Domain and Path must be provided
if c.URL == "" && (c.Domain == "" || c.Path == "") {
const msg = "if cookie URL is not provided, both domain and path must be specified: %#v"
return fmt.Errorf(msg, c)
}
// calculate the cookie expiration date, session cookie if not set.
var ts *cdp.TimeSinceEpoch
if c.Expires > 0 {
t := cdp.TimeSinceEpoch(time.Unix(c.Expires, 0))
ts = &t
}
cookiesToSet = append(cookiesToSet, &network.CookieParam{
Name: c.Name,
Value: c.Value,
Domain: c.Domain,
Path: c.Path,
URL: c.URL,
Expires: ts,
HTTPOnly: c.HTTPOnly,
Secure: c.Secure,
SameSite: network.CookieSameSite(c.SameSite),
})View on GitHub (pinned to 01ffac6f24)