Another Option: UI Automator

Yet another approach for testing Android applications is UI Automator. This is designed for integration testing, seeing how your app components integrate with the rest of a device, including integrating with other applications.

UI Automator, as the name suggests, automates UIs. It simulates user input, in the form of tapping on items and the like. Tests run by UI Automator are implemented in JUnit, but those tests have limited access to the widgets inside of a UI. Such access not only allows for directing simulated user input (e.g., “click the OK button”), but also for asserting that various test conditions are true (e.g., “does the list have five rows?”). In this respect, UI Automator behaves like traditional Android instrumented testing.

Wait! I Thought That Was Espresso’s Role!

Espresso also automates UI, simulating user input via JUnit tests. UI Automator tests even go inside the androidTest/ source set and can intermingle with instrumented test code.

The difference is that UI Automator can automate the system UI (e.g., “press the HOME button”) and automate the UIs of other apps (e.g., “click the Send button in the Google Messages UI”). This is very powerful, but it also has severe limits. The actual widgets reside in another process, not your own. As a result, we have only fairly crude forms of interacting with UIs and have even less ability to assert conditions in the other apps. Espresso gives us more control, but it can only automate our own app’s UI, and even then only within a single activity.

Scenarios for UIAutomator

UI Automator is mostly used for integration testing:

In these sorts of cases, you are not necessarily looking for a lot of details of the external activities. For example, in the last scenario, you will want to confirm that the third-party activity seems to have started and has received whatever information you are sending it (if any). But that is an activity from a third-party app, so most likely it is not your job to test all the functionality of that activity. UI Automator may suffice for your needs.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.