Exit course
Chapter 10 of 12 Intermediate 4 min read 1 quick check Updated

A Note on Mobile Locators

On this page
  1. No DOM, no CSS
  2. The mobile equivalents
  3. Finding locators on a device
  4. Summary

Everything so far was about web pages: a DOM, CSS, XPath over HTML, and roles computed by the browser. Native iOS and Android apps have none of the first three, and many web habits transfer badly. This chapter is a short map of what changes. The Appium with WebdriverIO course covers mobile locators hands-on.

No DOM, no CSS#

A native screen is a tree of platform UI elements (android.widget.Button, XCUIElementTypeButton), which Appium exposes as XML through the platform's accessibility and automation layers. CSS selectors do not apply at all. XPath works, because Appium can serialise the tree to XML, but that serialisation is the expensive part: on every XPath lookup the driver dumps the whole screen and then evaluates the expression, which on a large screen is far slower than a native query.

Mobile browsers are different: a web page in Safari on an iPhone or Chrome on Android has a normal DOM, and everything in chapters 3 to 8 applies unchanged. The same is true inside the webview context of a hybrid app.

The mobile equivalents#

Web habit Native equivalent Notes
data-testid accessibility id (content-desc on Android, accessibilityIdentifier on iOS) Cross-platform, fast, stable; the first choice
Hand-written id Android resource-id Usually stable across releases
Role and accessible name the element type plus its label or text Copy changes and translations break it, just as on the web
CSS for structure -android uiautomator selectors, -ios predicate string, -ios class chain Evaluated natively on the device, much faster than XPath
XPath XPath over the XML tree Last resort: slow, and absolute paths break on any layout change

The test id contract from chapter 6 carries over directly: on mobile it is a contract about accessibility identifiers. Agree on a naming scheme with the iOS and Android developers, use the same value on both platforms, and the same test code finds the element on both. Because accessibility ids feed into what assistive technology reads on Android, check with the developers which property they set, so that a test hook does not end up announced to screen reader users.

Finding locators on a device#

DevTools does not exist for native apps. The equivalent is an inspector that shows the element tree of the live app: Appium Inspector, or the inspector built into TestingBot's App Live, which you open from the DevTools icon during a live session, without any local setup. The same rules apply as in chapter 2: read the attributes around the element before copying, and rewrite generated XPath into an accessibility id or a native selector.

Our blog post on mobile locators for automated testing compares the strategies with examples, and the mobile app testing course covers choosing between Appium, Maestro, Espresso and XCUITest.

An Appium suite for an Android app uses XPath copied from the inspector everywhere, and each lookup takes over a second on large screens. What is the best first step?

CSS does not exist in a native context. A longer wait makes slow lookups slower. Shorter XPath is less brittle but still requires dumping the whole screen to XML on every lookup. Accessibility ids are resolved natively and work on both platforms.

Summary#

  • Native apps have no DOM and no CSS; XPath works but is slow and brittle.
  • Accessibility ids are the mobile test id: agree on them with developers, the same value on iOS and Android.
  • Mobile browsers and webviews are web pages, and the rest of this course applies to them.