grafana/k6 · error

missing required argument 'role'

Error message

missing required argument 'role'

What it means

Locator.getByRole(role, options) in the k6 browser module requires a non-nullish first argument naming an ARIA role; the mapping in locator_mapping.go throws before options are parsed. The role string is the primary selector input — options like name, checked, or level only refine the match. Playwright's getByRole is equally strict.

Source

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

				return nil, errors.New("missing required argument 'label'")
			}
			plabel, popts := parseGetByBaseOptions(vu.Context(), label, true, opts)

			ml := mapLocator(vu, lo.GetByLabel(plabel, popts))
			return rt.ToValue(ml).ToObject(rt), nil
		},
		"getByPlaceholder": func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) {
			if k6common.IsNullish(placeholder) {
				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'")

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass an ARIA role string first: page.locator('nav').getByRole('link', { name: 'Home' })
  2. Keep accessible-name criteria in options, not as a replacement for the role argument
  3. Validate/default the role variable before the call
  4. Verify the role constant or property is defined where it is declared

Example fix

// before
const home = page.locator('nav').getByRole(roleVar, { name: 'Home' }); // roleVar is undefined

// after
const home = page.locator('nav').getByRole('link', { name: 'Home' });
Defensive patterns

Strategy: validation

Validate before calling

if (!isAriaRole(role)) throw new Error(`getByRole needs a valid ARIA role, got: ${role}`);
locator.getByRole(role, opts);

Type guard

const ARIA_ROLES = new Set(['button','link','textbox','checkbox','radio','heading','img','list','listitem','navigation','main','banner','contentinfo','form','table','row','cell','dialog','alert','search','tab','tablist','option','combobox','menuitem','progressbar','slider','switch']);
function isAriaRole(v) {
  return typeof v === 'string' && ARIA_ROLES.has(v);
}

Try / catch

try {
  sub = locator.getByRole(maybeRole, opts);
} catch (e) {
  if (/missing required argument 'role'/.test(String(e.message))) {
    throw new Error('role must come first; use { name } in options for accessible names');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling page.locator('nav').getByRole() with no role, passing null/undefined, or moving the intended criterion into options ({ name: 'Home' }) while leaving the role undefined.

Common situations: Porting getByRole examples and dropping the role token, data-driven role configurations with gaps, optional role parameters in shared helpers.

Related errors


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