grafana/k6 · error
evaluating script on document: %w
Error message
evaluating script on document: %w
What it means
Page.evaluateOnNewDocument failed to register the script with CDP: the Page.addScriptToEvaluateOnNewDocument command returned an error. This runs when the browser module (or a context's addInitScript path) tries to evaluate a script on every new document, and the page's session could not execute the command.
Source
Thrown at internal/js/modules/k6/browser/common/page.go:637
}
func (p *Page) didClose() {
p.logger.Debugf("Page:didClose", "sid:%v", p.sessionID())
p.closedMu.Lock()
{
p.closed = true
}
p.closedMu.Unlock()
}
func (p *Page) evaluateOnNewDocument(source string) error {
p.logger.Debugf("Page:evaluateOnNewDocument", "sid:%v", p.sessionID())
action := page.AddScriptToEvaluateOnNewDocument(source)
_, err := action.Do(cdp.WithExecutor(p.ctx, p.session))
if err != nil {
return fmt.Errorf("evaluating script on document: %w", err)
}
return nil
}
func (p *Page) getFrameElement(f *Frame) (handle *ElementHandle, _ error) {
if f == nil {
p.logger.Debugf("Page:getFrameElement", "sid:%v frame:nil", p.sessionID())
} else {
p.logger.Debugf("Page:getFrameElement", "sid:%v fid:%s furl:%s",
p.sessionID(), f.ID(), f.URL())
}
parent := f.parentFrame
if parent == nil {
return nil, errors.New("frame has been detached 1")
}
View on GitHub (pinned to 93accf6570)
Solutions
- Call addInitScript before any goto()/newPage-dependent work, ideally right after context creation
- Await it so failures surface immediately rather than racing navigation
- If it appears at iteration end only, restructure to finish script setup before teardown starts
- Check for upstream session failures (DEBUG=k6-browser) when it appears mid-test
Example fix
// before
const page = await browser.newPage();
await page.goto('https://test.k6.io/');
await page.addInitScript ? null : null; // late registration path
// after
const ctx = browser.newContext();
ctx.addInitScript('window.marker = true;'); // registered on live session
const page = await ctx.newPage();
await page.goto('https://test.k6.io/'); Defensive patterns
Strategy: validation
Validate before calling
if (page.isClosed()) { throw new Error('page closed; init script registration will fail'); }
await ctx.addInitScript('window.marker = 1;'); Try / catch
try {
await ctx.addInitScript(src);
} catch (e) {
if (/evaluating script on document/.test(String(e))) return; // session gone; skip
throw e;
} Prevention
- Register init scripts before pages exist or before goto()
- Await addInitScript so failures surface immediately
- Keep init scripts small, valid JS — they run on every new document
When it happens
Trigger: Registering an init/evaluate-on-new-document script while the page session is closed or closing — e.g. addInitScript after page.close() began, or during teardown races at iteration end; or on a dead session after a browser crash.
Common situations: Late addInitScript calls racing context/page close; scripts injecting instrumentation right before navigation completes on a swapped target; running after an earlier crash made the session unusable.
Related errors
- internal error while applying init scripts to page: %w
- attaching new page: %w
- selectHandle.dispose: %w
- attaching worker target ID %v to session ID %v: %w
- disposing element with ID %s: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/f508e2fb0ffd50d2.
Report an issue: GitHub.