grafana/k6 · error

cookie value must be set: %#v

Error message

cookie value must be set: %#v

What it means

Validation error from BrowserContext.AddCookies: a cookie in the list has an empty Value. Together with the name check, k6 requires both fields to be non-empty strings before it will forward cookies to the browser via CDP Storage.setCookies; the failing cookie is printed with %#v.

Source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:469

}

// AddCookies adds cookies into this browser context.
// All pages within this context will have these cookies installed.
func (b *BrowserContext) AddCookies(cookies []*Cookie) error {
	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,

View on GitHub (pinned to 93accf6570)

Solutions

  1. Populate value with a non-empty string, e.g. from the Set-Cookie response header of your login request.
  2. If the value is legitimately empty in your flow, wait until the cookie is actually set before harvesting and re-adding it.
  3. Filter or assert on cookie values before calling addCookies when the data source is dynamic.

Example fix

// before
const sessionCookie = { name: 'sid', value: loginResponse.cookies.sid || '', url: 'https://example.com' };
context.addCookies([sessionCookie]); // empty value -> error

// after
if (!loginResponse.cookies.sid) throw new Error('login did not set sid cookie');
context.addCookies([{ name: 'sid', value: loginResponse.cookies.sid[0].value, url: 'https://example.com' }]);
Defensive patterns

Strategy: validation

Validate before calling

if (!cookies.every((c) => typeof c.value === 'string' && c.value !== '')) {
  throw new Error('every cookie needs a non-empty "value"');
}
context.addCookies(cookies);

Type guard

function isFilledCookie(c) {
  return typeof c.name === 'string' && c.name !== '' &&
         typeof c.value === 'string' && c.value !== '';
}

Prevention

When it happens

Trigger: Calling browserContext.addCookies with a cookie object missing the value key, or with value: '' — including values that arrive empty from a previous step, e.g. reading document.cookie of a page that has not set the cookie yet, or a login response that returned no session token.

Common situations: Session-injection patterns where cookies are harvested from one context/page and replayed into another, but the harvest happened too early; API changes on the target site leaving the cookie value blank; misspelled 'value' key in a fixture.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/02eab7f1774875c4. Report an issue: GitHub.