Skip to content

UiAutomator Locator Strategy

The UiAutomator2 driver enables element lookup using UiSelector and UiScrollable locator types, which are natively supported by the UI Automator framework. These locators allow for flexible ways to reference complicated element paths, and their performance is very close to native.

The driver enables the use of locator types via the -android uiautomator locator strategy.

Examples

Note that the index selector is unreliable so prefer instance instead. The following examples are written against the ApiDemos application using Ruby.

  • Find the first textview:

    # ruby
    first_textview = find_element(:uiautomator, 'new UiSelector().className("android.widget.TextView").instance(0)');
    
  • Find the first element by text:

    # ruby
    first_text = find_element(:uiautomator, 'new UiSelector().text("Animation")')
    first_text.text # "Animation"
    
  • Find the first scrollable element, then find a TextView with the text Tabs. The Tabs element will be scrolled into view.

    # ruby
    element = find_element(:uiautomator, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "Tabs")')
    
  • As a special case, scrollIntoView returns the element that is scrolled into view. It allows scrolling to any UiSelector.

    # ruby
    element = find_element(:uiautomator, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("WebView").instance(0));')
    element.text # "WebView"
    

More Resources