Skip to main content

Locators

getByRole() lets a single test target the same element on both Android and iOS, even though each platform names its native classes differently. Mobilewright does this by normalizing the native type reported by the device and mapping it to a semantic role.

Where each locator looks​

Every element in the UI dump carries a handful of attributes. Each getBy…() locator reads one of them, but Android and iOS fill those attributes from different native properties.

The same text field on both platforms​

Here is one text field from the Mobilewright playground app (Basic UI screen), as each platform's accessibility layer reports it. The user has typed into it.

Android (AccessibilityNodeInfo):

{
"class": "android.widget.EditText",
"text": "Hello World",
"hint": "Text Field",
"content-desc": "text_field",
"resource-id": "com.mobilenext.playground:id/text_field",
"enabled": true,
"focused": true
}

iOS (XCUITest element snapshot):

{
"elementType": "XCUIElementTypeTextField",
"identifier": "text_field",
"label": "",
"value": "hello",
"placeholderValue": "Enter text",
"enabled": true,
"hasFocus": true
}

Which native property feeds which locator​

AttributeAndroid sourceiOS sourceLocator / assertion
typeclasselementTypegetByRole() (normalized), getByType() (raw)
labelcontent-desc (contentDescription)label (accessibilityLabel)getByLabel(), getByRole(…, { name })
testIdresource-id (android:id)identifier (accessibilityIdentifier)getByTestId()
texttextnever reportedgetByText(), toHaveText()
valueslider position (RangeInfo), text field contentvalue (accessibilityValue)toHaveValue(), getValue()
placeholderhint (hintText)placeholderValuegetByPlaceholder()
checkedcheckedswitch value of "1"toBeChecked()

A few rules that follow from this table:

  • getByText() falls back. It matches the first non-empty of text, label, then value. iOS never reports text, so on iOS getByText() matches the label, or for a text field without a label, what the user typed.
  • getByRole(…, { name }) reads the label first, then text, and for some roles the children's text (see Accessible names). A text field without a label of its own is labelled by its children, as a Jetpack Compose TextField is. The iOS text field above has neither, so it has no name: match it by role alone or by another locator.
  • getByTestId() accepts the short Android id. getByTestId('text_field') matches com.mobilenext.playground:id/text_field, so one test id works on both platforms.
  • There is no getByValue(). A value changes as the user interacts, so find the element another way and assert on it with toHaveValue().
  • Not every accessibility property is visible to tests. XCUITest does not expose accessibilityHint or accessibilityTraits, and Android's tooltipText and stateDescription are not reported, so no locator can match them.

Finding the text field​

Each of these finds the field above (Basic UI has three text fields on Android and two on iOS):

// test id: identical on both platforms
screen.getByTestId('text_field');

// role: the native class, normalized. Without a name it matches every text field
screen.getByRole('textfield').first();
screen.getByRole('textfield', { name: 'text_field' }); // Android only: iOS has no label here

// label: Android content-desc / iOS accessibilityLabel
screen.getByLabel('text_field'); // Android only, for the same reason

// placeholder: Android hint / iOS placeholderValue
screen.getByPlaceholder('Text Field'); // Android
screen.getByPlaceholder('Enter text'); // iOS

// text: Android text / iOS falls back to value
screen.getByText('Hello World'); // Android
screen.getByText('hello'); // iOS

And to check what the user typed:

const field = screen.getByTestId('text_field');
await expect(field).toHaveValue('hello');

Prefer getByTestId(), then getByRole() with a name, for locators that survive copy changes and work on both platforms. Placeholder and text differ between the two apps here, which is common, so they make weaker cross-platform locators.

How a native type becomes a role​

When you call screen.getByRole('textfield'), the query engine:

  1. Takes the raw native type from the UI dump (Android: android.widget.EditText, iOS: XCUIElementTypeTextField).
  2. Normalizes it — strips the Android package prefix (android.widget., androidx.*, …) and the iOS XCUIElementType prefix — leaving a bare name (edittext, textfield).
  3. Matches that bare name against the role's class list.

getByType() skips normalization and matches the raw native class instead, so use it (or getByLabel() / getByTestId()) for classes that have no role mapping.

Android class → role​

Android reports the base framework class (e.g. android.widget.EditText, even when the app uses AppCompatEditText).

Native classgetByRole()
android.widget.Buttonbutton
android.widget.ImageButtonbutton
android.widget.EditTexttextfield
android.widget.TextViewtext
android.widget.ImageViewimage
android.widget.Switchswitch
android.widget.ToggleButtonswitch
android.widget.CheckBoxcheckbox
android.widget.RadioButtonradio
android.widget.SeekBarslider
android.widget.ProgressBarprogressbar
android.widget.Spinnercombobox
android.widget.ListViewlist
androidx.recyclerview.widget.RecyclerViewlist
android.widget.ScrollViewlist
android.widget.LinearLayoutlistitem
android.widget.RelativeLayoutlistitem
android.widget.Toolbarheader

AndroidX / Material Components​

Some dumps surface the AndroidX or Material Components subclass name directly instead of the collapsed framework class above — these are also recognized:

Native classgetByRole()
androidx.appcompat.widget.AppCompatButtonbutton
com.google.android.material.button.MaterialButtonbutton
com.google.android.material.floatingactionbutton.FloatingActionButtonbutton
androidx.appcompat.widget.AppCompatRadioButtonradio
androidx.appcompat.widget.AppCompatSpinnercombobox
androidx.appcompat.widget.AppCompatEditTexttextfield
com.google.android.material.textfield.TextInputEditTexttextfield
androidx.appcompat.widget.AppCompatTextViewtext
com.google.android.material.textview.MaterialTextViewtext
androidx.appcompat.widget.AppCompatImageViewimage
com.google.android.material.imageview.ShapeableImageViewimage

React Native (Android)​

Native classgetByRole()
…textinput.ReactEditTexttextfield
…text.ReactTextViewtext
…image.ReactImageViewimage
…scroll.ReactScrollViewlist
…view.ReactViewGroupbutton — only when clickable="true" or accessible="true"

Jetpack Compose (Android)​

Compose reports most of its widgets as a plain android.view.View. It only reports android.widget.Button for a button with no children, so a Button { Text("Save") } arrives as a clickable View with its caption on a child TextView.

Compose nodegetByRole()
android.view.Viewbutton — only when clickable
android.view.Viewalert — only when its pane title is Dialog, as on a Material3 AlertDialog

A Compose TextField / OutlinedTextField is a real android.widget.EditText (textfield), but its floating label = { Text("Email") } is a child TextView. A text field without a label of its own is labelled by that child, so both of these find it:

await screen.getByLabel('Email').fill('glorpax@milliways.space');
await expect(screen.getByRole('textfield', { name: 'Email' })).toHaveValue('glorpax@milliways.space');

iOS class → role​

Native classgetByRole()
XCUIElementTypeButtonbutton
XCUIElementTypeTextFieldtextfield
XCUIElementTypeSecureTextFieldtextfield
XCUIElementTypeSearchFieldtextfield
XCUIElementTypeStaticTexttext
XCUIElementTypeTextViewtext — see note below
XCUIElementTypeImageimage
XCUIElementTypeSwitchswitch
XCUIElementTypeSliderslider
XCUIElementTypeProgressIndicatorprogressbar
XCUIElementTypeActivityIndicatorprogressbar
XCUIElementTypeAlertalert
XCUIElementTypeSheetalert
XCUIElementTypePickercombobox
XCUIElementTypePickerWheelcombobox
XCUIElementTypeTablelist
XCUIElementTypeCollectionViewlist
XCUIElementTypeScrollViewlist
XCUIElementTypeCelllistitem
XCUIElementTypeOtherlistitem
XCUIElementTypeTabtab
XCUIElementTypeTabBartab
XCUIElementTypeLinklink
XCUIElementTypeNavigationBarheader

Accessible names​

getByRole('button', { name }) matches the element's own label, then its own text. Roles that take their name from their content — button, link, tab, header, switch, checkbox and radio, as in ARIA — then fall back to the text of their children. That is how a Compose button is named by its caption:

await screen.getByRole('button', { name: 'Save' }).tap();

Other roles are never named by their children, so a list isn't named by its items. The one exception is a textfield, which is named by its floating label (see above).

Notes and known gaps​

  • Classes with no role. Anything not listed above has no role mapping — getByRole() won't find it. Target it with getByType('<raw.native.Class>'), getByLabel(), or getByTestId(). Examples: Android RadioGroup, CheckedTextView; iOS DatePicker.

  • iOS source filtering. mobilecli keeps the iOS dump small, so not every element reaches the query engine. Button, TextField, SecureTextField, SearchField, TextView, Switch, WebView, Slider, Picker and PickerWheel are always included. Every other class in the iOS table is included only when it has a label, name or accessibilityIdentifier — so getByRole('list') finds a labeled Table but not an anonymous one. XCUIElementTypeOther is included only when it carries an accessibilityIdentifier. Requires mobilecli 1.0.13 or later; earlier versions only surface Button, TextField, SecureTextField, SearchField, TextView, Switch, WebView, StaticText, Image and Icon.

  • TextView is platform-ambiguous. On Android, TextView is a static label (text). On iOS, XCUIElementTypeTextView is an editable multiline input (UITextView / SwiftUI TextEditor), which is closer to textfield. Both normalize to the same textview token, so they currently share the text role. Aligning an editable iOS text view with textfield (matching the web/ARIA model, where <textarea> and <input> are both textbox) is tracked separately.