grafana/k6 · error

missing required argument 'text'

Error message

missing required argument 'text'

What it means

Thrown by the Frame mapping's getByText method in k6's browser module. The first argument (the text to match) is mandatory and is validated on the Go side with k6common.IsNullish before a locator is built, mirroring Playwright's API contract. Any nullish JS value (undefined, null) passed as `text` triggers this error.

Source

Thrown at internal/js/modules/k6/browser/browser/locator_mapping.go:281

				return nil, errors.New("missing required argument 'role'")
			}
			popts := parseGetByRoleOptions(vu.Context(), opts)

			ml := mapLocator(vu, lo.GetByRole(role.String(), popts))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"getByTestId": func(testID sobek.Value) (*sobek.Object, error) {
			if k6common.IsNullish(testID) {
				return nil, errors.New("missing required argument 'testId'")
			}
			ptestID := parseStringOrRegex(testID, false)

			ml := mapLocator(vu, lo.GetByTestID(ptestID))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"getByText": func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) {
			if k6common.IsNullish(text) {
				return nil, errors.New("missing required argument 'text'")
			}
			ptext, popts := parseGetByBaseOptions(vu.Context(), text, true, opts)

			ml := mapLocator(vu, lo.GetByText(ptext, popts))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"getByTitle": func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) {
			if k6common.IsNullish(title) {
				return nil, errors.New("missing required argument 'title'")
			}
			ptitle, popts := parseGetByBaseOptions(vu.Context(), title, false, opts)

			ml := mapLocator(vu, lo.GetByTitle(ptitle, popts))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"locator": func(selector string, opts sobek.Value) mapping {
			return mapLocator(vu, lo.Locator(selector, parseLocatorOptions(rt, opts)))
		},

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass a string or RegExp as the first argument: frame.getByText('Submit') or frame.getByText(/submit/i)
  2. If the value comes from data, default or guard it first: frame.getByText(text ?? 'Submit'), or throw a descriptive error when it is missing
  3. Audit the call site for variables that can be undefined (destructuring, optional JSON fields) before they reach getByText

Example fix

// before
const text;
frame.getByText(text);

// after
frame.getByText(text ?? 'Submit')
Defensive patterns

Strategy: validation

Validate before calling

if (text === undefined || text === null) {
  throw new Error(`frame.getByText: 'text' is required, got ${text}`);
}
const loc = frame.getByText(text);

Type guard

function isTextMatcher(v) {
  return typeof v === 'string' || v instanceof RegExp;
}

Try / catch

try {
  frame.getByText(text);
} catch (e) {
  if (!e.message.includes("missing required argument 'text'")) throw e;
  throw new Error(`test data missing text value: ${text}`); // rethrow with context
}

Prevention

When it happens

Trigger: Calling frame.getByText() with no arguments, or passing undefined/null explicitly: frame.getByText(undefined), or a variable that was never assigned (const text; frame.getByText(text)).

Common situations: Test data drives the text (CSV, env var) and the value is empty/unset; refactoring from page.$('text=Submit') to getByText and forgetting the argument; copy-pasting Playwright snippets that assumed an optional first parameter.

Related errors


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