grafana/k6 · error
emulating media: %w
Error message
emulating media: %w
What it means
Page.EmulateMedia stores media type / color scheme / reduced motion settings and applies them via Emulation.setEmulatedMedia in every frame session. This error means at least one frame session's CDP call failed while iterating under the frame-sessions read lock.
Source
Thrown at internal/js/modules/k6/browser/common/page.go:1076
func (p *Page) DispatchEvent(selector string, typ string, eventInit any, opts *FrameDispatchEventOptions) error {
p.logger.Debugf("Page:DispatchEvent", "sid:%v selector:%s", p.sessionID(), selector)
return p.MainFrame().DispatchEvent(selector, typ, eventInit, opts)
}
// EmulateMedia emulates the given media type.
func (p *Page) EmulateMedia(popts *PageEmulateMediaOptions) error {
p.logger.Debugf("Page:EmulateMedia", "sid:%v", p.sessionID())
p.mediaType = popts.Media
p.colorScheme = popts.ColorScheme
p.reducedMotion = popts.ReducedMotion
p.frameSessionsMu.RLock()
for _, fs := range p.frameSessions {
if err := fs.updateEmulateMedia(); err != nil {
p.frameSessionsMu.RUnlock()
return fmt.Errorf("emulating media: %w", err)
}
}
p.frameSessionsMu.RUnlock()
applySlowMo(p.ctx)
return nil
}
// EmulateVisionDeficiency activates/deactivates emulation of a vision deficiency.
func (p *Page) EmulateVisionDeficiency(typ string) error {
p.logger.Debugf("Page:EmulateVisionDeficiency", "sid:%v typ:%s", p.sessionID(), typ)
validTypes := map[string]emulation.SetEmulatedVisionDeficiencyType{
"achromatopsia": emulation.SetEmulatedVisionDeficiencyTypeAchromatopsia,
"blurredVision": emulation.SetEmulatedVisionDeficiencyTypeBlurredVision,
"deuteranopia": emulation.SetEmulatedVisionDeficiencyTypeDeuteranopia,
"none": emulation.SetEmulatedVisionDeficiencyTypeNone,View on GitHub (pinned to 93accf6570)
Solutions
- Call page.emulateMedia before navigation or after a stable load state
- Avoid calling it concurrently with page.goto() (await each call)
- Ensure the page is not closing when the call is made
- Apply emulation at browser-context level (newContext({colorScheme: ...})) for pages with unstable iframes
Example fix
// before
await page.goto('https://example.com/');
await page.emulateMedia({ media: 'print' }); // races with iframe attach
// after
await page.emulateMedia({ media: 'print' });
await page.goto('https://example.com/'); Defensive patterns
Strategy: try-catch
Validate before calling
const opts = { media: 'print', colorScheme: 'dark', reducedMotion: 'reduce' };
if (!['print','screen','null',''].includes(opts.media)) throw new Error('bad media'); Try / catch
try {
await page.emulateMedia(opts);
} catch (e) {
if (/emulating media/.test(e.message)) { await page.waitForLoadState(); await page.emulateMedia(opts); }
else throw e;
} Prevention
- Apply media emulation before goto, or after a stable load state
- Never call emulateMedia concurrently with navigation calls
When it happens
Trigger: page.emulateMedia({ media: 'print', colorScheme: 'dark', reducedMotion: 'reduce' }) while iframes are attaching/detaching, during navigation, or on a page whose session is closing.
Common situations: Emulating print media on pages with ad/analytics iframes that churn; calling emulateMedia concurrently with page.goto; page already closing at iteration end.
Related errors
- getting iframe frame: %w
- getting remote node %q: %w
- internal error while initializing frame %T: %w
- internal error while updating emulated media: %w
- getting frame owner: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/84288f341eab5bc1.
Report an issue: GitHub.