grafana/k6 · error

missing required argument 'testId'

Error message

missing required argument 'testId'

What it means

Locator.getByTestId(testId) in the k6 browser module 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 is the entire selector within the locator scope. Playwright's method has the same contract.

Source

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

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

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

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass the test id string or RegExp: page.locator('tbody').getByTestId('row-42')
  2. Verify the constant is imported and defined before use (log it once)
  3. Use a RegExp for generated ids: page.locator('tbody').getByTestId(/^row-\d+$/)
  4. Fix missing exports or stale names in the selectors module

Example fix

// before
const row = page.locator('tbody').getByTestId(SEL.row); // SEL.row is undefined

// after
const row = page.locator('tbody').getByTestId('row-42');
Defensive patterns

Strategy: validation

Validate before calling

const tid = SEL.row(rowNum);
if (!isTextSelector(tid)) throw new Error(`test id for row ${rowNum} resolved to nothing`);
locator.getByTestId(tid);

Type guard

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

Try / catch

try {
  sub = locator.getByTestId(maybeTid);
} catch (e) {
  if (/missing required argument 'testId'/.test(String(e.message))) {
    throw new Error(`testId missing; SEL map lacks an entry for this entity`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling page.locator('tbody').getByTestId() with no argument, passing null/undefined, or forwarding a selector constant that is undefined after a rename or missing re-export.

Common situations: Table row scoping with per-row generated ids, selector-module refactors, helpers receiving an optional id parameter.

Related errors


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