grafana/k6 · error

internal error while updating emulated media: %w

Error message

internal error while updating emulated media: %w

What it means

page.emulateMedia() (media type, colorScheme, reducedMotion) is implemented with the CDP command Emulation.setEmulatedMedia. This error is raised when that CDP command itself fails; the message says 'internal error' because the failure is at the protocol/session layer, not in your argument values, which are validated and mapped to emulation.MediaFeature before the call.

Source

Thrown at internal/js/modules/k6/browser/common/frame_session.go:1134

		features = append(features, &emulation.MediaFeature{Name: "prefers-color-scheme", Value: "light"})
	case ColorSchemeDark:
		features = append(features, &emulation.MediaFeature{Name: "prefers-color-scheme", Value: "dark"})
	default:
		features = append(features, &emulation.MediaFeature{Name: "prefers-color-scheme", Value: ""})
	}

	switch fs.page.reducedMotion {
	case ReducedMotionReduce:
		features = append(features, &emulation.MediaFeature{Name: "prefers-reduced-motion", Value: "reduce"})
	default:
		features = append(features, &emulation.MediaFeature{Name: "prefers-reduced-motion", Value: ""})
	}

	action := emulation.SetEmulatedMedia().
		WithMedia(string(fs.page.mediaType)).
		WithFeatures(features)
	if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
		return fmt.Errorf("internal error while updating emulated media: %w", err)
	}
	return nil
}

func (fs *FrameSession) updateExtraHTTPHeaders(initial bool) error {
	fs.logger.Debugf("NewFrameSession:updateExtraHTTPHeaders", "sid:%v tid:%v", fs.session.ID(), fs.targetID)

	// Merge extra headers from browser context and page, where page specific headers ake precedence.
	mergedHeaders := make(network.Headers)
	for k, v := range fs.page.browserCtx.opts.ExtraHTTPHeaders {
		mergedHeaders[k] = v
	}
	for k, v := range fs.page.extraHTTPHeaders {
		mergedHeaders[k] = v
	}
	if !initial || len(mergedHeaders) > 0 {
		if err := fs.networkManager.SetExtraHTTPHeaders(mergedHeaders); err != nil {
			return fmt.Errorf("updating extra HTTP headers: %w", err)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Call page.emulateMedia() while the page is definitely open, ideally right after page.goto() completes
  2. Guard the call with page.isClosed() and skip emulation during teardown
  3. Retry the call once after a short delay if the failure coincided with navigation
  4. If it fails consistently, inspect browser logs for a crashed process and fix the environment (memory, sandbox flags)

Example fix

// before
await page.emulateMedia({ media: 'print' }); // may run after navigation/close

// after
if (!page.isClosed()) {
  await page.emulateMedia({ media: 'print' });
}
Defensive patterns

Strategy: validation

Validate before calling

if (!page.isClosed()) {
  await page.emulateMedia({ media: 'print' });
}

Try / catch

try { await page.emulateMedia({ media: 'print' }); }
catch (e) { if (!/emulated media/.test(String(e.message))) throw e; }

Prevention

When it happens

Trigger: Calling page.emulateMedia({ media: 'print' }) (or configuring reducedMotion/colorScheme on the browser context or page) after the page target has started closing, after the browser DevTools connection dropped, or while the CDP session for the frame is invalid.

Common situations: Emulating print media or reduced motion before screenshots/PDF generation; calling emulateMedia in teardown or after page.close(); long-running tests where the browser disconnects; browser crash caused by resource exhaustion under load.

Related errors


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