UI Automator
UI Automator
UI Automator is a UI testing framework introduced by Google to facilitate automation on an Android emulator or device. UI Automator is especially useful for cross-app testing, where your test needs to interact with multiple apps or system UI components
Appium leverages this UIAutomator with its own wrapper and came up with UIAutomator2 Driver to automate the Android applications.
In short:
- UI Automator (v1) is Google’s official Android testing framework for instrumented UI tests.
- UI Automator2 is not an official Google framework—it’s Appium’s Android automation driver that uses UI Automator (and Android Accessibility) under the hood but exposes a WebDriver-compatible interface.
Features
Some of the features are as follows:
- Cross-app interaction: Test interactions between your app and other apps or system UI.
- Device-wide UI access: Access UI elements outside your app’s package (e.g., status bar, home screen).
- Robust selectors: Locate UI elements using properties like text, content description, class name, resource ID, etc.
- Supports Android 4.3 (API 18) and higher.
- Works well with JUnit 4 and AndroidJUnitRunner
Core Components
- UiDevice
Represents the device state. You can use it to:- Press hardware buttons (
) - Take screenshots (
) - Access system dialogs
- Press hardware buttons (
- UiObject
Represents a UI element (e.g., button, text field). You can:- Click, long-click, drag
- Get text or properties
- Wait for existence
- UiCollection
Enumerates UI elements for searching within a container. - UiScrollable
Enables scrolling to find a target UI element (e.g., in a ListView or RecyclerView). - UiSelector
Used to query for specific UI elements using attributes:
UI Automator vs UI Automator2
Some of the differences are as follows:
| UI Automator (v1) | UI Automator2 | |
|---|---|---|
| Framework Type | Android native testing framework (part of Android SDK) | Appium’s custom automation driver built on top of UI Automator (v1) and later Android Accessibility APIs |
| Maintained By | Google (as part of Android Testing Support Library) | Appium (Open-source project, not Google) |
| Primary Use Case | Instrumented Android UI tests (run via AndroidJUnitRunner) | Remote automation of Android apps via WebDriver protocol (used with Appium) |
| Execution Environment | Runs directly on Android device/emulator as an instrumentation test | Runs as a background server (UiAutomator2 driver) on device; controlled remotely via Appium server |
| Language Support | Java/Kotlin (Android test code) | Any language that supports WebDriver (e.g., Python, Java, JavaScript, C#, Ruby) |
| Test Runner | AndroidJUnitRunner | Appium (uses UiAutomator2 as its Android automation backend) |
| API Access | Direct access to UiDevice, UiObject, etc. |
Access via WebDriver JSON Wire Protocol / W3C WebDriver commands |
| Cross-App Testing | Yes (native support) | Yes (inherited from UI Automator v1 capabilities) |
| Support for Android Versions | API 18+ (Android 4.3+) | API 21+ (Android 5.0+) — better support for modern Android versions |
| Performance & Stability | Stable but limited to instrumentation context | Improved stability, faster element location, better handling of async UI (e.g., RecyclerView) |
| Element Finding | Uses UiSelector with Java API |
Uses XPath, ID, accessibility ID, class name, etc. via WebDriver locators |
| Debugging & Logs | Logcat + Android Studio test logs | Appium server logs + device logs; easier remote debugging |
| Integration | Integrated with Android Studio and Gradle | Integrated with Appium, Selenium Grid, CI/CD pipelines (cross-platform) |
| Open Source | Yes (part of AndroidX) | Yes (part of Appium project on GitHub) |