{"record":{"id":"a4507e6889369be2","repo":"grafana/k6","slug":"setting-content-w","errorCode":null,"errorMessage":"setting content: %w","messagePattern":"setting content: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":1783,"sourceCode":"\n\t// TODO(@inancgumus): Respect the FrameSetContentOptions before executing the action.\n\t// A solution is similar to the logic in `WaitForLoadState`.\n\n\tjs := `(html) => {\n\t\twindow.stop();\n\t\tdocument.open();\n\t\tdocument.write(html);\n\t\tdocument.close();\n\t}`\n\n\tf.waitForExecutionContext(utilityWorld)\n\n\teopts := evalOptions{\n\t\tforceCallable: true,\n\t\treturnByValue: true,\n\t}\n\tif _, err := f.evaluate(f.ctx, utilityWorld, eopts, js, html); err != nil {\n\t\treturn fmt.Errorf(\"setting content: %w\", err)\n\t}\n\n\tapplySlowMo(f.ctx)\n\n\treturn nil\n}\n\n// SetInputFiles sets input files for the selected element.\nfunc (f *Frame) SetInputFiles(selector string, pfiles *Files, popts *FrameSetInputFilesOptions) error {\n\tf.log.Debugf(\"Frame:SetInputFiles\", \"fid:%s furl:%q sel:%q\", f.ID(), f.URL(), selector)\n\n\tif err := f.setInputFiles(selector, pfiles, popts); err != nil {\n\t\treturn fmt.Errorf(\"setting input files on %q: %w\", selector, err)\n\t}\n\n\tapplySlowMo(f.ctx)\n\n\treturn nil","sourceCodeStart":1765,"sourceCodeEnd":1801,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/frame.go#L1765-L1801","documentation":"Thrown by k6 browser's frame.SetContent(html): it waits for the utility-world execution context, then evaluates document.open()/write()/close() with the new HTML. The error means that evaluation failed — the context was destroyed or the session died mid-write.","triggerScenarios":"page.setContent(html) while the page is navigating (the context it waited for gets replaced), on a closed page, or when a race between waitForExecutionContext and the evaluate destroys the context.","commonSituations":"Calling setContent immediately after goto() without waiting for load; another frame/action triggering navigation concurrently; calling setContent on a page already closed by test end.","solutions":["Settle navigation first: page.waitForLoadState('load') (or waitForNavigation) before setContent","Check page.isClosed() and reopen the page if needed","Retry setContent once after a settle — the race is transient","Prefer goto('data:text/html,...') or a fixture URL when the content is fully static"],"exampleFix":"// before\npage.setContent('<h1>hello</h1>');\n\n// after\npage.waitForLoadState('load');\nlet ok = false;\nfor (let i = 0; i < 2 && !ok; i++) {\n  try { page.setContent('<h1>hello</h1>'); ok = true; }\n  catch (e) { page.waitForLoadState('load'); }\n}","handlingStrategy":"retry","validationCode":"if (page.isClosed()) throw new Error('page closed; cannot setContent');\npage.waitForLoadState('load');","typeGuard":null,"tryCatchPattern":"const setContentSafe = (page, html, tries = 3) => {\n  for (let i = 0; i < tries; i++) {\n    try { page.setContent(html); return; }\n    catch (e) {\n      if (i === tries - 1) throw e;\n      page.waitForLoadState('load');\n    }\n  }\n};","preventionTips":["Never call setContent while a goto/navigation is still committing","For static fixtures prefer goto with a data: URL or served fixture over setContent"],"tags":["k6","browser","set-content","navigation","execution-context"],"backgroundTag":"execution-context-destroyed","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}