Locator Strategies
The UiAutomator2 driver supports several location strategies in the native context. The list below describes each strategy along with usage recommendations.
Id¶
| Name | Speed Ranking | Example |
|---|---|---|
id |
⭐⭐⭐⭐⭐ | com.mycompany:id/resourceId |
This strategy is mapped to the native UI Automator BySelector.res
matcher, which returns elements whose resource name matches the specified value exactly.
If the package identifier prefix is not specified and the disableIdLocatorAutocompletion
setting is not enabled, the prefix identifier of the current app under test is added automatically.
When to Use
Use this strategy for elements that can be identified using their View.id
attribute value.
Accessibility Id¶
| Name | Speed Ranking | Example |
|---|---|---|
accessibility id |
⭐⭐⭐⭐⭐ | my description |
This strategy is mapped to the native UI Automator BySelector.desc
matcher, which returns elements whose content description matches the specified value exactly.
When to Use
Use this strategy for elements that can be identified using their View.contentDescription
attribute value.
Class Name¶
| Name | Speed Ranking | Example |
|---|---|---|
class name |
⭐⭐⭐⭐⭐ | android.view.View |
This strategy is mapped to the native UI Automator BySelector.clazz
matcher, which returns elements whose class name matches the specified value exactly.
When to Use
Use this strategy for elements that can be identified using their class name.
UiAutomator¶
| Name | Speed Ranking | Example |
|---|---|---|
-android uiautomator |
⭐⭐⭐⭐ | new UiSelector().resourceId("android:id/list") |
This strategy is mapped to the native UI Automator UiSelector locator.
The strategy also allows to implicitly perform some advanced operations, like scrolling. Refer to the UiAutomator Locator Strategy guide for more details.
Warning
Google is planning to deprecate
and remove UiCollection, UiObject, UiScrollable, and UiSelector support from the
UiAutomator framework. This will render all -android uiautomator-based locators invalid, so
keep this in mind while using them or planning to use them in the future.
When to Use
Use this strategy for elements that cannot be directly identified by their class name, id,
or contentDescription.
XPath¶
| Name | Speed Ranking | Example |
|---|---|---|
xpath |
⭐⭐⭐ | //android.view.View[@text="Regular" and @checkable="true"] |
This strategy relies on the XML tree generated by the UiAutomator2 driver's page source API.
XPath versions 1.0 and 2.0 are supported.
When to Use
Use this strategy when no other strategy is applicable, typically for elements that can only be identified from their sibling or descendant elements.