grafana/k6 · error

cookie name must be set: %#v

Error message

cookie name must be set: %#v

What it means

Client-side validation in AddCookies: every cookie must have a non-empty Name before the Storage.setCookies CDP call. The offending cookie is printed with %#v so you can see all its fields. No network traffic happens — the batch is rejected locally.

Solutions

  1. Ensure every cookie object has a non-empty name string
  2. Validate the array before the call: every item needs name, value, and url or (domain and path)
  3. Log the cookie objects before calling to spot mis-keyed or empty fields

Example fix

// before
ctx.addCookies([{ nme: 'session', value: 'abc', url: 'https://example.com' }]); // typo: nme

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

Strategy: validation

Validate before calling

function validateCookies(cookies) {
  for (const c of cookies) {
    if (typeof c.name !== 'string' || c.name === '') {
      throw new Error(`cookie missing name: ${JSON.stringify(c)}`);
    }
  }
}
validateCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);
ctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);

Type guard

const isNamedCookie = (c) =>
  c != null && typeof c === 'object' && typeof c.name === 'string' && c.name.length > 0;

Prevention

When it happens

Trigger: browserContext.addCookies([{ value: 'v', url: 'https://example.com' }]) — name omitted; name built from a variable that is undefined/empty (e.g. a typo'd property like nme); name is an empty string from parsed data.

Common situations: Building cookies dynamically from HTTP responses or fixtures where the name field is missing or mis-keyed; destructuring mistakes when mapping Set-Cookie strings; cookies constructed from config with optional fields.

Related errors


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

Appendix: 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 01ffac6f24)