grafana/k6 · error

setting HTTP credentials in target ID

Error message

setting HTTP credentials in target ID %s: %w

What it means

Thrown by the deprecated SetHTTPCredentials: it stores credentials on the context and then calls updateHTTPCredentials (Network.setExtraHTTPHeaders) on every existing page; the page named by targetID failed. k6 also logs a deprecation warning because this API is scheduled for removal — the replacement is the httpCredentials option of browser.newContext().

Solutions

  1. Replace setHTTPCredentials with browser.newContext({ httpCredentials: { username, password } }) — this is the supported path and applies before pages exist
  2. If you must use the deprecated call, do it immediately after newContext() while no pages are open
  3. Ensure pages in the context are alive when the call runs
  4. Wrap in try/catch if it lives on a cleanup path

Example fix

// before
const ctx = browser.newContext();
const page = ctx.newPage();
ctx.setHTTPCredentials({ username: 'u', password: 'p' }); // deprecated, per-page update can fail

// after
const ctx = browser.newContext({ httpCredentials: { username: 'u', password: 'p' } });
const page = ctx.newPage();
Defensive patterns

Strategy: fallback

Validate before calling

// supported replacement: credentials via context options, applied before pages exist
const ctx = browser.newContext({
  httpCredentials: { username: 'u', password: 'p' },
});
const page = ctx.newPage();

Prevention

When it happens

Trigger: browserContext.setHTTPCredentials({ username, password }) while some page in the context has a dead CDP session, or after the browser connection dropped; calling it after pages were closed.

Common situations: Legacy scripts from before the httpCredentials option existed; basic-auth setups where credentials change mid-run; cleanup ordering where the call lands after teardown of pages.

Related errors


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

Appendix: source

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

	return nil
}

// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.
//
// Deprecated: Create a new BrowserContext with httpCredentials instead.
// See for details:
// - https://github.com/microsoft/playwright/issues/2196#issuecomment-627134837
// - https://github.com/microsoft/playwright/pull/2763
func (b *BrowserContext) SetHTTPCredentials(hc Credentials) error {
	b.logger.Warnf("setHTTPCredentials", "setHTTPCredentials is deprecated."+
		" Create a new BrowserContext with httpCredentials instead.")
	b.logger.Debugf("BrowserContext:SetHTTPCredentials", "bctxid:%v", b.id)

	b.opts.HTTPCredentials = hc
	for _, p := range b.browser.getPages() {
		if err := p.updateHTTPCredentials(); err != nil {
			return fmt.Errorf("setting HTTP credentials in target ID %s: %w", p.targetID, err)
		}
	}

	return nil
}

// SetOffline toggles the browser's connectivity on/off.
func (b *BrowserContext) SetOffline(offline bool) error {
	b.logger.Debugf("BrowserContext:SetOffline", "bctxid:%v offline:%t", b.id, offline)

	b.opts.Offline = offline
	for _, p := range b.browser.getPages() {
		if err := p.updateOffline(); err != nil {
			return fmt.Errorf(
				"setting offline status to %t for the browser context ID %s: %w",
				offline, b.id, err,
			)
		}

View on GitHub (pinned to 01ffac6f24)