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 nil

View on GitHub (pinned to 93accf6570)

Solutions

  1. Wait for the frame to be idle before setContent: await frame.waitForLoadState('load') (or waitForNavigation to settle redirects).
  2. Use a fresh frame/page reference; check page.isClosed() first.
  3. Raise the k6 iteration timeout (options thresholds / per-VU timeout) if context cancellation is the cause.
  4. 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

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


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