grafana/k6 · error
setting content: %w
Error message
setting content: %w
What it means
Frame.SetContent() evaluates document.open()/write()/close() with your HTML in the frame's utility world; any evaluation failure is wrapped as 'setting content: <cause>'. Common causes are a missing/destroyed execution context (frame navigating or detaching), a closed page, or a context cancelled by the k6 iteration timeout.
Source
Thrown at internal/js/modules/k6/browser/common/frame.go:1775
// TODO(@inancgumus): Respect the FrameSetContentOptions before executing the action.
// A solution is similar to the logic in `WaitForLoadState`.
js := `(html) => {
window.stop();
document.open();
document.write(html);
document.close();
}`
f.waitForExecutionContext(utilityWorld)
eopts := evalOptions{
forceCallable: true,
returnByValue: true,
}
if _, err := f.evaluate(f.ctx, utilityWorld, eopts, js, html); err != nil {
return fmt.Errorf("setting content: %w", err)
}
applySlowMo(f.ctx)
return nil
}
// SetInputFiles sets input files for the selected element.
func (f *Frame) SetInputFiles(selector string, pfiles *Files, popts *FrameSetInputFilesOptions) error {
f.log.Debugf("Frame:SetInputFiles", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)
if err := f.setInputFiles(selector, pfiles, popts); err != nil {
return fmt.Errorf("setting input files on %q: %w", selector, err)
}
applySlowMo(f.ctx)
return nilView on GitHub (pinned to 93accf6570)
Solutions
- Wait for the frame to be idle before setContent: await frame.waitForLoadState('load') (or waitForNavigation to settle redirects).
- Use a fresh frame/page reference; check page.isClosed() first.
- Raise the k6 iteration timeout (options thresholds / per-VU timeout) if context cancellation is the cause.
- For iframes, ensure the parent document finished loading so the utility world exists.
Example fix
// before
await page.goto(url);
await page.setContent(html); // redirect still in flight
// after
await page.goto(url);
await page.waitForLoadState('load');
await page.setContent(html); Defensive patterns
Strategy: retry
Validate before calling
if (page.isClosed()) throw new Error('page closed');
await page.waitForLoadState('load'); Try / catch
try { await page.setContent(html); }
catch (e) {
if (/setting content/.test(e.message)) { await page.waitForLoadState('load'); return page.setContent(html); }
throw e;
} Prevention
- Wait for redirects/load to settle before setContent
- Check page.isClosed() and frame detachment first
- Size k6 iteration timeouts above worst-case page load
When it happens
Trigger: frame.setContent(html) while the frame is mid-navigation; calling setContent on a detached frame or after page.close(); the k6 iteration was cancelled (per-iteration timeout reached) so f.ctx was done.
Common situations: setContent racing an automatic navigation (redirect chains after goto); reuse of stale frame handles; tight iteration timeouts in load tests causing context cancellation.
Related errors
- getting document element handle: %w
- getting document: %w
- getting frame title: %w
- missing required argument 'url'
- getting injected script: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/a4507e6889369be2.
Report an issue: GitHub.