{"record":{"id":"2a7b12154e4a43ab","repo":"grafana/k6","slug":"evaluate-requires-a-page-function","errorCode":null,"errorMessage":"evaluate requires a page function","messagePattern":"evaluate requires a page function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/frame_mapping.go","lineNumber":73,"sourceCode":"\t\t\t\treturn nil, fmt.Errorf(\"parsing double click options: %w\", err)\n\t\t\t}\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, f.Dblclick(selector, popts) //nolint:wrapcheck\n\t\t\t}), nil\n\t\t},\n\t\t\"dispatchEvent\": func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) {\n\t\t\tpopts := common.NewFrameDispatchEventOptions(f.Timeout())\n\t\t\tif err := popts.Parse(vu.Context(), opts); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing frame dispatch event options: %w\", err)\n\t\t\t}\n\t\t\tearg := exportArg(eventInit)\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, f.DispatchEvent(selector, typ, earg, popts) //nolint:wrapcheck\n\t\t\t}), nil\n\t\t},\n\t\t\"evaluate\": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) {\n\t\t\tif sobekEmptyString(pageFunc) {\n\t\t\t\treturn nil, fmt.Errorf(\"evaluate requires a page function\")\n\t\t\t}\n\t\t\tfuncString := pageFunc.String()\n\t\t\tgopts := exportArgs(gargs)\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn f.Evaluate(funcString, gopts...)\n\t\t\t}), nil\n\t\t},\n\t\t\"evaluateHandle\": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) {\n\t\t\tif sobekEmptyString(pageFunc) {\n\t\t\t\treturn nil, fmt.Errorf(\"evaluateHandle requires a page function\")\n\t\t\t}\n\t\t\tfuncString := pageFunc.String()\n\t\t\tgopts := exportArgs(gargs)\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\tjsh, err := f.EvaluateHandle(funcString, gopts...)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err //nolint:wrapcheck\n\t\t\t\t}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/browser/frame_mapping.go#L55-L91","documentation":"frame.evaluate(pageFunc, ...args) was called with a nullish, empty, or whitespace-only page function (frame_mapping.go:73, checked via sobekEmptyString which tests IsNullish or a trimmed-empty string). k6 requires the first argument to be a non-empty function or string before it can serialize it for in-page execution.","triggerScenarios":"frame.evaluate(), frame.evaluate(''), frame.evaluate('   '), or frame.evaluate(someVar) where someVar is undefined - the check fails before a promise is created and the error is thrown synchronously.","commonSituations":"Building the function from a template string that rendered empty; passing a variable before it was assigned; typos like frame.evaluate(arg1) where the caller forgot the function; refactoring that moved the function into a helper that returns undefined.","solutions":["Pass a function: frame.evaluate(() => document.title) or a non-empty string: frame.evaluate('document.title')","If the expression is dynamic, guard it: if (!fn || !String(fn).trim()) throw new Error('empty page function')","Check that the variable holding the function is defined at call time (undefined module import, missing return)"],"exampleFix":"// before\nconst expr = '';\nframe.evaluate(expr);\n// after\nconst expr = 'document.title';\nframe.evaluate(expr);","handlingStrategy":"type-guard","validationCode":"const fn = typeof pageFunc === 'function' ? pageFunc.toString()\n          : typeof pageFunc === 'string' ? pageFunc.trim() : '';\nif (!fn) throw new Error('frame.evaluate needs a non-empty function or string');","typeGuard":"function isUsablePageFunction(v) {\n  if (v == null) return false;\n  if (typeof v === 'function') return true;\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  await frame.evaluate(fn, ...args);\n} catch (e) {\n  console.error(`evaluate failed: ${e.message}`);\n}","preventionTips":["Always pass an inline arrow function or a non-empty string literal","Fail fast with your own clear message when building the function dynamically","Log the serialized function before sending when debugging empty-expression errors"],"tags":["k6","browser","frame","evaluate","javascript"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}