grafana/k6 · error

missing required argument 'testId'

Error message

missing required argument 'testId'

What it means

Frame.getByTestId(testId) on a k6 browser Frame requires a non-nullish argument; the mapping layer rejects missing, null, or undefined values before calling parseStringOrRegex. The test id (string or RegExp) is matched against the data-testid attribute and constitutes the entire selector. Playwright's method has the same contract.

Source

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

				return nil, errors.New("missing required argument 'placeholder'")
			}
			pplaceholder, popts := parseGetByBaseOptions(vu.Context(), placeholder, false, opts)

			ml := mapLocator(vu, f.GetByPlaceholder(pplaceholder, popts))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"getByRole": func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) {
			if k6common.IsNullish(role) {
				return nil, errors.New("missing required argument 'role'")
			}
			popts := parseGetByRoleOptions(vu.Context(), opts)

			ml := mapLocator(vu, f.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, f.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, f.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'")

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass the test id string or RegExp: frame.getByTestId('cart-item-42')
  2. Verify the selector constant is imported and defined (console.log it before use)
  3. Use a RegExp for generated ids: frame.getByTestId(/^cart-item-\d+$/)
  4. Fix missing exports or renamed constants in the selectors module

Example fix

// before
import { TID } from './selectors.js';
const item = page.frame('#shop').getByTestId(TID.cartItem); // undefined after a rename

// after
const item = page.frame('#shop').getByTestId('cart-item');
Defensive patterns

Strategy: validation

Validate before calling

const tid = SEL[entity];
if (!isTextSelector(tid)) throw new Error(`SEL.${entity} is missing or empty`);
frame.getByTestId(tid);

Type guard

function isTextSelector(v) {
  return (typeof v === 'string' && v.length > 0) || v instanceof RegExp;
}

Try / catch

try {
  loc = frame.getByTestId(maybeTid);
} catch (e) {
  if (/missing required argument 'testId'/.test(String(e.message))) {
    throw new Error(`testId missing for entity '${entity}'; selector map may be stale`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling frame.getByTestId() with no argument, passing null/undefined, or forwarding a selector constant that is undefined due to a rename or missing re-export.

Common situations: Centralized selector modules after refactoring, generated test ids with dynamic suffixes handled incorrectly, or assertion helpers receiving an optional id.

Related errors


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