{"record":{"id":"0c388c2f1106eeec","repo":"grafana/k6","slug":"evaluating-js-w","errorCode":null,"errorMessage":"evaluating JS: %w","messagePattern":"evaluating JS: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":868,"sourceCode":"\n\treturn nil\n}\n\n// EvaluateWithContext will evaluate provided page function within an execution context.\n// The passed in context will be used instead of the frame's context. The context must\n// be a derivative of one that contains the sobek runtime.\nfunc (f *Frame) EvaluateWithContext(ctx context.Context, pageFunc string, args ...any) (any, error) {\n\tf.log.Debugf(\"Frame:EvaluateWithContext\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\tf.waitForExecutionContext(mainWorld)\n\n\topts := evalOptions{\n\t\tforceCallable: true,\n\t\treturnByValue: true,\n\t}\n\tresult, err := f.evaluate(ctx, mainWorld, opts, pageFunc, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"evaluating JS: %w\", err)\n\t}\n\n\tapplySlowMo(ctx)\n\n\treturn result, nil\n}\n\n// Evaluate will evaluate provided page function within an execution context.\nfunc (f *Frame) Evaluate(pageFunc string, args ...any) (any, error) {\n\tf.log.Debugf(\"Frame:Evaluate\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\treturn f.EvaluateWithContext(f.ctx, pageFunc, args...)\n}\n\n// EvaluateGlobal will evaluate the given JS code in the global object.\nfunc (f *Frame) EvaluateGlobal(ctx context.Context, js string) error {\n\taction := runtime.Evaluate(js).WithAwaitPromise(true)\n","sourceCodeStart":850,"sourceCodeEnd":886,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L850-L886","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\nframe.evaluate(`(${id}) => document.getElementById(${id}).value`); // id interpolated unquoted -> syntax error\n\n// after\nframe.evaluate('(id) => document.getElementById(id).value', 'username');","handlingStrategy":"try-catch","validationCode":"// prove the snippet is valid JS before the run\nnew Function('(arg) => { ' + pageFuncBody + ' }'); // throws at build time on syntax errors\nawait page.waitForLoadState('domcontentloaded');","typeGuard":null,"tryCatchPattern":"try {\n  const v = frame.evaluate(pageFunc, ...args);\n} catch (e) {\n  console.warn('evaluate failed:', e.message);\n}","preventionTips":["Prototype every pageFunc in DevTools first.","Pass data through args, never interpolation.","Depend waits on the DOM state the snippet assumes."],"tags":["browser","evaluate","javascript","serialization","execution-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}