{"record":{"id":"cf9059bc349b94d0","repo":"grafana/k6","slug":"getting-injected-script-w-cf9059","errorCode":null,"errorMessage":"getting injected script: %w","messagePattern":"getting injected script: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":1991,"sourceCode":"\tpolling any, timeout time.Duration, args ...any,\n) (any, error) {\n\tf.log.Debugf(\n\t\t\"Frame:waitForFunction\",\n\t\t\"fid:%s furl:%q world:%s poll:%s timeout:%s\",\n\t\tf.ID(), f.URL(), world, polling, timeout)\n\n\tf.waitForExecutionContext(world)\n\n\tf.executionContextMu.RLock()\n\tdefer f.executionContextMu.RUnlock()\n\n\texecCtx := f.executionContexts[world]\n\tif execCtx == nil {\n\t\treturn nil, fmt.Errorf(\"waiting for function: execution context %q not found\", world)\n\t}\n\tinjected, err := execCtx.getInjectedScript(apiCtx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting injected script: %w\", err)\n\t}\n\n\tpageFn := `\n\t\t(injected, predicate, polling, timeout, ...args) => {\n\t\t\treturn injected.waitForPredicateFunction(predicate, polling, timeout, ...args);\n\t\t}\n\t`\n\n\t// First evaluate the predicate function itself to get its handle.\n\topts := evalOptions{forceCallable: false, returnByValue: false}\n\thandle, err := execCtx.eval(apiCtx, opts, js)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"waiting for function, getting handle: %w\", err)\n\t}\n\n\t// Then evaluate the injected function call, passing it the predicate\n\t// function handle and the rest of the arguments.\n\topts = evalOptions{forceCallable: true, returnByValue: false}","sourceCodeStart":1973,"sourceCodeEnd":2009,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L1973-L2009","documentation":"Inside waitForFunction, k6 evaluates the injected-script expression (injectedScript.js utility) in the frame's execution world; failure to evaluate/serialize it is wrapped as 'getting injected script: <cause>'. This is an internal prerequisite step — the user's predicate has not run yet — and fails when the context is unusable or the injected script source cannot be fetched.","triggerScenarios":"The execution context found earlier is destroyed between the lookup and the eval (navigation race); Page.addScriptTag/evaluate of the utility fails because the renderer is gone; CDP call erroring with context destroyed. Immediately follows error 817's conditions when they develop mid-call.","commonSituations":"Fast-navigating SPAs during waitForFunction polling; iframes torn down while a poll is starting; browser under memory pressure crashing renderers.","solutions":["Retry the waitForFunction once after waiting for a stable load state — transient context destruction self-heals after navigation completes.","Start polling only after domcontentloaded/load to avoid the navigation window.","If recurring, capture DEBUG=br* logs — an injected-script fetch failure is a k6 issue worth reporting with the log."],"exampleFix":"// before\nawait frame.waitForFunction(fn);\n\n// after\nawait frame.waitForLoadState('domcontentloaded');\nawait frame.waitForFunction(fn).catch(async (e) => {\n  await frame.waitForLoadState('load');\n  return frame.waitForFunction(fn);\n});","handlingStrategy":"retry","validationCode":"await frame.waitForLoadState('domcontentloaded');","typeGuard":null,"tryCatchPattern":"try { return await frame.waitForFunction(fn, arg, { timeout }); }\ncatch (e) {\n  if (/getting injected script/.test(e.message)) {\n    await frame.waitForLoadState('load');\n    return frame.waitForFunction(fn, arg, { timeout });\n  }\n  throw e;\n}","preventionTips":["Start polls after load state, not during redirects","Retry once after settling — context destruction is transient","Report with DEBUG logs if it reproduces on static pages"],"tags":["browser","wait","injected-script","execution-context","navigation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}