grafana/k6 · error
evaluating JS: %w
Error message
evaluating JS: %w
What it means
Frame.EvaluateWithContext() waits for the main-world execution context and calls f.evaluate with forceCallable and returnByValue. This error wraps any failure: the evaluated function threw an exception in the page, an argument failed to serialize, the context was destroyed by navigation, or a CDP protocol error occurred.
Source
Thrown at internal/js/modules/k6/browser/common/frame.go:868
return nil
}
// EvaluateWithContext will evaluate provided page function within an execution context.
// The passed in context will be used instead of the frame's context. The context must
// be a derivative of one that contains the sobek runtime.
func (f *Frame) EvaluateWithContext(ctx context.Context, pageFunc string, args ...any) (any, error) {
f.log.Debugf("Frame:EvaluateWithContext", "fid:%s furl:%q", f.ID(), f.URL())
f.waitForExecutionContext(mainWorld)
opts := evalOptions{
forceCallable: true,
returnByValue: true,
}
result, err := f.evaluate(ctx, mainWorld, opts, pageFunc, args...)
if err != nil {
return nil, fmt.Errorf("evaluating JS: %w", err)
}
applySlowMo(ctx)
return result, nil
}
// 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)
View on GitHub (pinned to 93accf6570)
Solutions
- Run the same expression in the browser DevTools console first to prove it evaluates cleanly.
- Pass arguments via args..., not string interpolation into pageFunc.
- Wait for the page/element state the expression depends on (waitForSelector/waitForLoadState).
Example fix
// before
frame.evaluate(`(${id}) => document.getElementById(${id}).value`); // id interpolated unquoted -> syntax error
// after
frame.evaluate('(id) => document.getElementById(id).value', 'username'); Defensive patterns
Strategy: try-catch
Validate before calling
// prove the snippet is valid JS before the run
new Function('(arg) => { ' + pageFuncBody + ' }'); // throws at build time on syntax errors
await page.waitForLoadState('domcontentloaded'); Try / catch
try {
const v = frame.evaluate(pageFunc, ...args);
} catch (e) {
console.warn('evaluate failed:', e.message);
} Prevention
- Prototype every pageFunc in DevTools first.
- Pass data through args, never interpolation.
- Depend waits on the DOM state the snippet assumes.
When it happens
Trigger: pageFunc string is not valid JS or throws at runtime (undefined variable, null deref); args contain non-serializable values; page navigates mid-evaluate; frame detached.
Common situations: Passing a JS snippet that references DOM nodes not yet present; mixing quoted strings into the function string (breaking syntax); evaluating during redirects; passing functions or complex objects as args.
Related errors
- evaluate requires a page function
- converting argument %q in execution context ID %d and frame
- %s
- extracting value from remote object with ID %s: %w
- evaluating JS in global context: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/0c388c2f1106eeec.
Report an issue: GitHub.