{"record":{"id":"8ad3e782cc27704d","repo":"grafana/k6","slug":"internal-error-while-updating-emulated-media-w","errorCode":null,"errorMessage":"internal error while updating emulated media: %w","messagePattern":"internal error while updating emulated media: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame_session.go","lineNumber":1134,"sourceCode":"\t\tfeatures = append(features, &emulation.MediaFeature{Name: \"prefers-color-scheme\", Value: \"light\"})\n\tcase ColorSchemeDark:\n\t\tfeatures = append(features, &emulation.MediaFeature{Name: \"prefers-color-scheme\", Value: \"dark\"})\n\tdefault:\n\t\tfeatures = append(features, &emulation.MediaFeature{Name: \"prefers-color-scheme\", Value: \"\"})\n\t}\n\n\tswitch fs.page.reducedMotion {\n\tcase ReducedMotionReduce:\n\t\tfeatures = append(features, &emulation.MediaFeature{Name: \"prefers-reduced-motion\", Value: \"reduce\"})\n\tdefault:\n\t\tfeatures = append(features, &emulation.MediaFeature{Name: \"prefers-reduced-motion\", Value: \"\"})\n\t}\n\n\taction := emulation.SetEmulatedMedia().\n\t\tWithMedia(string(fs.page.mediaType)).\n\t\tWithFeatures(features)\n\tif err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {\n\t\treturn fmt.Errorf(\"internal error while updating emulated media: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (fs *FrameSession) updateExtraHTTPHeaders(initial bool) error {\n\tfs.logger.Debugf(\"NewFrameSession:updateExtraHTTPHeaders\", \"sid:%v tid:%v\", fs.session.ID(), fs.targetID)\n\n\t// Merge extra headers from browser context and page, where page specific headers ake precedence.\n\tmergedHeaders := make(network.Headers)\n\tfor k, v := range fs.page.browserCtx.opts.ExtraHTTPHeaders {\n\t\tmergedHeaders[k] = v\n\t}\n\tfor k, v := range fs.page.extraHTTPHeaders {\n\t\tmergedHeaders[k] = v\n\t}\n\tif !initial || len(mergedHeaders) > 0 {\n\t\tif err := fs.networkManager.SetExtraHTTPHeaders(mergedHeaders); err != nil {\n\t\t\treturn fmt.Errorf(\"updating extra HTTP headers: %w\", err)","sourceCodeStart":1116,"sourceCodeEnd":1152,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame_session.go#L1116-L1152","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call page.emulateMedia() while the page is definitely open, ideally right after page.goto() completes","Guard the call with page.isClosed() and skip emulation during teardown","Retry the call once after a short delay if the failure coincided with navigation","If it fails consistently, inspect browser logs for a crashed process and fix the environment (memory, sandbox flags)"],"exampleFix":"// before\nawait page.emulateMedia({ media: 'print' }); // may run after navigation/close\n\n// after\nif (!page.isClosed()) {\n  await page.emulateMedia({ media: 'print' });\n}","handlingStrategy":"validation","validationCode":"if (!page.isClosed()) {\n  await page.emulateMedia({ media: 'print' });\n}","typeGuard":null,"tryCatchPattern":"try { await page.emulateMedia({ media: 'print' }); }\ncatch (e) { if (!/emulated media/.test(String(e.message))) throw e; }","preventionTips":["Call emulateMedia right after goto completes, never in teardown","Guard page-state APIs with page.isClosed()","Apply media/reducedMotion/colorScheme as browser context options at creation time so k6 applies them during setup"],"tags":["browser","emulation","cdp","emulate-media"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}