grafana/k6 · error

cookie name must be set: %#v

Error message

cookie name must be set: %#v

What it means

Validation error from BrowserContext.AddCookies: a cookie in the provided list has an empty name field. Before converting cookies to CDP network.CookieParam, k6 requires every cookie to have a non-empty Name; the offending cookie struct is printed with %#v so you can identify it. The whole addCookies call fails atomically.

Source

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

func (b *BrowserContext) getSession(id target.SessionID) *Session {
	return b.browser.conn.getSession(id)
}

// 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,

View on GitHub (pinned to 93accf6570)

Solutions

  1. Set a non-empty name on every cookie: { name: 'session', value: '...', url: 'https://example.com' }.
  2. Check for exact, lowercase field names: name, value, url, domain, path, expires, httpOnly, secure, sameSite.
  3. Validate/filter the array before the call when cookies come from external data.

Example fix

// before
context.addCookies([{ nane: 'session', value: 'abc', url: 'https://example.com' }]); // typo -> empty name

// after
context.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

function isCookieWithName(c) {
  return c != null && typeof c === 'object' && typeof c.name === 'string' && c.name !== ''; 
}

Prevention

When it happens

Trigger: Calling browserContext.addCookies([{ value: 'abc', url: 'https://example.com' }]) or with a cookie whose name is undefined/null/empty string (undefined struct fields unmarshal to empty in the JS-to-Go mapping).

Common situations: Typos in the cookie object keys (e.g. key 'Name' or 'cookieName' instead of 'name' — keys are case-sensitive per the js tags); building cookies dynamically where the name comes from a variable that is sometimes empty; data-driven tests consuming malformed cookie fixtures.

Related errors


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