grafana/k6 · error

cookie value must be set: %#v

Error message

cookie value must be set: %#v

What it means

Client-side validation in AddCookies: a cookie with a non-empty name was given an empty Value, and k6 requires a non-empty value even though empty-valued cookies technically exist in HTTP. The cookie is printed with %#v. The batch is rejected before any CDP call.

Solutions

  1. Give every cookie a non-empty value, even a placeholder like '-' if the test only needs the name present
  2. Validate the array up front: name and value must be non-empty strings
  3. Fix the upstream extraction so empty values are filtered out or defaulted

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: browserContext.addCookies([{ name: 'session', url: '...' }]) with value omitted; value computed from a response header or variable that turned out empty; value field mis-keyed (e.g. val).

Common situations: Mapping Set-Cookie headers where some cookie had no '=' value; values derived from JSON fixtures with missing properties; intentionally trying to set a valueless flag-style cookie (not supported here).

Related errors


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

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