grafana/k6 · error

setting HTTP credentials in target ID %s: %w

Error message

setting HTTP credentials in target ID %s: %w

What it means

Thrown by the deprecated BrowserContext.SetHTTPCredentials when it fails to push new HTTP credentials to one of the open pages in the context. After storing the credentials in the context options, the method iterates every page and calls updateHTTPCredentials, which sends a CDP emulation command to each frame session; if that CDP call fails, the error is wrapped with the failing page's target ID. The method itself is deprecated because Playwright removed it: credentials are now fixed at context creation.

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 93accf6570)

Solutions

  1. Replace the deprecated call: close the context and create a new one with browser.newContext({ httpCredentials: { username, password } }) (or launchPersistentContext), which is the supported approach.
  2. If you must keep the call, verify the browser connection and pages are alive first (no prior crash in logs) and that the error is not caused by an already-closed target.
  3. Check the wrapped error (%w) in the message to see which target ID failed and inspect k6 debug logs (--log-output=debug or DEBUG=true) for the underlying CDP failure.

Example fix

// before
const context = browser.newContext();
context.setHTTPCredentials({ username: 'user', password: 'pass' });

// after
const context = browser.newContext({
  httpCredentials: { username: 'user', password: 'pass' },
});
Defensive patterns

Strategy: try-catch

Try / catch

try {
  context.setHTTPCredentials({ username, password });
} catch (e) {
  // Prefer migrating off the deprecated API instead of handling this.
  console.error(`setHTTPCredentials failed (target closed?): ${e.message}`);
}

Prevention

When it happens

Trigger: Calling browserContext.setHTTPCredentials({username, password}) on a context whose pages/frame sessions have a broken or closed CDP connection (browser crashed, page target closed mid-update, browser disconnected). The error can also surface when the k6 browser process is being torn down while the call is in flight.

Common situations: Scripts ported from Puppeteer or older k6 browser versions that set credentials after context creation; calling setHTTPCredentials after the browser or one of its pages has already closed; flaky headless Chrome environments (memory pressure, container OOM kills) that drop the CDP websocket.

Related errors


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