{"record":{"id":"c2ec6eef7d81da1a","repo":"grafana/k6","slug":"evaluating-js-in-global-context-w","errorCode":null,"errorMessage":"evaluating JS in global context: %w","messagePattern":"evaluating JS in global context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":892,"sourceCode":"}\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\n\tvar (\n\t\texceptionDetails *runtime.ExceptionDetails\n\t\terr              error\n\t)\n\tif _, exceptionDetails, err = action.Do(cdp.WithExecutor(ctx, f.manager.session)); err != nil {\n\t\treturn fmt.Errorf(\"evaluating JS in global context: %w\", err)\n\t}\n\tif exceptionDetails != nil {\n\t\treturn fmt.Errorf(\"%s\", parseExceptionDetails(exceptionDetails))\n\t}\n\n\treturn nil\n}\n\n// EvaluateHandle will evaluate provided page function within an execution context.\nfunc (f *Frame) EvaluateHandle(pageFunc string, args ...any) (handle JSHandleAPI, _ error) {\n\tf.log.Debugf(\"Frame:EvaluateHandle\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\tevalHandle := func() (JSHandleAPI, error) {\n\t\tf.executionContextMu.RLock()\n\t\tdefer f.executionContextMu.RUnlock()\n\n\t\tec := f.executionContexts[mainWorld]\n\t\tif ec == nil {","sourceCodeStart":874,"sourceCodeEnd":910,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L874-L910","documentation":"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.","triggerScenarios":"Calling EvaluateGlobal while the page is navigating or closing; the browser process crashed; the frame's session was detached (iframe swapped).","commonSituations":"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.","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."],"exampleFix":"// before\nconst page = await browser.newPage();\npage.evaluateGlobal('window.__stub = true'); // too early / mid-nav\n\n// after\nconst page = await browser.newPage();\nawait page.goto(url);\nawait page.waitForLoadState('domcontentloaded');\npage.evaluateGlobal('window.__stub = true');","handlingStrategy":"retry","validationCode":"await page.goto(url);\nawait page.waitForLoadState('domcontentloaded');","typeGuard":null,"tryCatchPattern":"for (let i = 0; i < 3; i++) {\n  try { page.evaluateGlobal(js); return; } catch (e) { await sleep(500); }\n}\nthrow new Error('evaluateGlobal failed after retries');","preventionTips":["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."],"tags":["browser","evaluate","cdp","execution-context","navigation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}