grafana/k6 · error

evaluate requires a page function

Error message

evaluate requires a page function

What it means

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.

Source

Thrown at internal/js/modules/k6/browser/browser/frame_mapping.go:73

				return nil, fmt.Errorf("parsing double click options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, f.Dblclick(selector, popts) //nolint:wrapcheck
			}), nil
		},
		"dispatchEvent": func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) {
			popts := common.NewFrameDispatchEventOptions(f.Timeout())
			if err := popts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing frame dispatch event options: %w", err)
			}
			earg := exportArg(eventInit)
			return promise(vu, func() (any, error) {
				return nil, f.DispatchEvent(selector, typ, earg, popts) //nolint:wrapcheck
			}), nil
		},
		"evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) {
			if sobekEmptyString(pageFunc) {
				return nil, fmt.Errorf("evaluate requires a page function")
			}
			funcString := pageFunc.String()
			gopts := exportArgs(gargs)
			return promise(vu, func() (any, error) {
				return f.Evaluate(funcString, gopts...)
			}), nil
		},
		"evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) {
			if sobekEmptyString(pageFunc) {
				return nil, fmt.Errorf("evaluateHandle requires a page function")
			}
			funcString := pageFunc.String()
			gopts := exportArgs(gargs)
			return promise(vu, func() (any, error) {
				jsh, err := f.EvaluateHandle(funcString, gopts...)
				if err != nil {
					return nil, err //nolint:wrapcheck
				}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass a function: frame.evaluate(() => document.title) or a non-empty string: frame.evaluate('document.title')
  2. If the expression is dynamic, guard it: if (!fn || !String(fn).trim()) throw new Error('empty page function')
  3. Check that the variable holding the function is defined at call time (undefined module import, missing return)

Example fix

// before
const expr = '';
frame.evaluate(expr);
// after
const expr = 'document.title';
frame.evaluate(expr);
Defensive patterns

Strategy: type-guard

Validate before calling

const fn = typeof pageFunc === 'function' ? pageFunc.toString()
          : typeof pageFunc === 'string' ? pageFunc.trim() : '';
if (!fn) throw new Error('frame.evaluate needs a non-empty function or string');

Type guard

function isUsablePageFunction(v) {
  if (v == null) return false;
  if (typeof v === 'function') return true;
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  await frame.evaluate(fn, ...args);
} catch (e) {
  console.error(`evaluate failed: ${e.message}`);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/2a7b12154e4a43ab. Report an issue: GitHub.