grafana/k6 · error
evaluating JS in global context: %w
Error message
evaluating JS in global context: %w
What it means
Frame.EvaluateGlobal() executes raw JS via CDP runtime.Evaluate on the frame's session (not a page-function call). This error wraps the protocol-level Do() failure: the CDP session/target is closed or the execution context is gone — distinct from a JS exception, which is handled separately.
Source
Thrown at internal/js/modules/k6/browser/common/frame.go:892
}
// Evaluate will evaluate provided page function within an execution context.
func (f *Frame) Evaluate(pageFunc string, args ...any) (any, error) {
f.log.Debugf("Frame:Evaluate", "fid:%s furl:%q", f.ID(), f.URL())
return f.EvaluateWithContext(f.ctx, pageFunc, args...)
}
// EvaluateGlobal will evaluate the given JS code in the global object.
func (f *Frame) EvaluateGlobal(ctx context.Context, js string) error {
action := runtime.Evaluate(js).WithAwaitPromise(true)
var (
exceptionDetails *runtime.ExceptionDetails
err error
)
if _, exceptionDetails, err = action.Do(cdp.WithExecutor(ctx, f.manager.session)); err != nil {
return fmt.Errorf("evaluating JS in global context: %w", err)
}
if exceptionDetails != nil {
return fmt.Errorf("%s", parseExceptionDetails(exceptionDetails))
}
return nil
}
// EvaluateHandle will evaluate provided page function within an execution context.
func (f *Frame) EvaluateHandle(pageFunc string, args ...any) (handle JSHandleAPI, _ error) {
f.log.Debugf("Frame:EvaluateHandle", "fid:%s furl:%q", f.ID(), f.URL())
evalHandle := func() (JSHandleAPI, error) {
f.executionContextMu.RLock()
defer f.executionContextMu.RUnlock()
ec := f.executionContexts[mainWorld]
if ec == nil {View on GitHub (pinned to 93accf6570)
Solutions
- Ensure a document exists first: waitForLoadState('domcontentloaded') before EvaluateGlobal.
- Retry once after navigation settles.
- Check that the page/browser is still open and earlier steps did not close it.
Example fix
// before
const page = await browser.newPage();
page.evaluateGlobal('window.__stub = true'); // too early / mid-nav
// after
const page = await browser.newPage();
await page.goto(url);
await page.waitForLoadState('domcontentloaded');
page.evaluateGlobal('window.__stub = true'); Defensive patterns
Strategy: retry
Validate before calling
await page.goto(url);
await page.waitForLoadState('domcontentloaded'); Try / catch
for (let i = 0; i < 3; i++) {
try { page.evaluateGlobal(js); return; } catch (e) { await sleep(500); }
}
throw new Error('evaluateGlobal failed after retries'); Prevention
- Run global stubs only after a document exists.
- Avoid evaluating during navigation or after page.close().
- Watch for browser-crash signals earlier in the iteration.
When it happens
Trigger: Calling EvaluateGlobal while the page is navigating or closing; the browser process crashed; the frame's session was detached (iframe swapped).
Common situations: Installing global stubs (window.fetch mocks) right at page creation before any document exists; evaluating during teardown after page.close(); crashed browser under memory pressure in CI.
Related errors
- errorText
- getting node in frame: %w
- no frame found for node: %w
- getting injected script: %w
- expected node but got %s
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/c2ec6eef7d81da1a.
Report an issue: GitHub.