Select Your Favourite
Category And Start Learning.

Espresso & UIAutomator – the perfect tandem

Espresso for Android is perfect and fast test automation framework, but it has one important limitation – you are allowed to operate only inside your app under test context.

That means that it is not possible to automate tests for such app features like:

  • Application push notifications.
  • Contact synchronization.
  • Navigating from another app to your app under test.

Since you have to deal with other apps from the mobile device – Notification Bar, Contacts or People app, etc.

In fact it wasn’t possible until the release of UIAutomator 2.0. As stated in Android Developers blog post – “…Most importantly, UI Automator is now based on Android Instrumentation…”. And because of that we can run UIAutomator tests as well as Espresso tests using Instrumentation test runner.

In addition to that we can combine UIAutomator tests together with Espresso tests and this gives us the real power and control over the phone and application under test.

In the below example it will be explained how this approach was used to automate Contact Synchronisation tests:

public class TestContactsSync {

    public ActivityTestRule mActivityRule = 
        new ActivityTestRule(LoginActivity.class);

    UiDevice mDevice;
    String PEOPLE_APP = "People";
    String MY_APP = "XING";
    String userContactName = "Android Tester";

    public void setUp() throws Exception{
        mDevice = 

    public void testContactsSync() throws Exception {

        // Espresso: login the user, navigate to contact sync 
        // and enable clicking on toggle

        // UIAutomator: navigate to People app 
        UiObject conutactsApp = 
            mDevice.findObject(new UiSelector().text(PEOPLE_APP));

        // UIAutomator: check that contact is present after sync was triggered    
        UiObject contactName = 
            mDevice.findObject(new UiSelector().text(userContactName));

        // UIAutomator: navigate back to my app under testm
        UiObject myApp = mDevice.findObject(new UiSelector().text(MY_APP));

        // Espresso: turn off contact sync and logout

    public void tearDown() throws Exception {

Isn’t it perfect?! And the answer is – almost. UIAutomator requires Android 4.3 (API level 18) or higher. And this can be solved easily by tweaking build.gradle file – adding productFlavors and declare uiautomator dependencies:

productFlavors {
    // The actual application flavor 
    production {
        minSdkVersion 14
    // Test application flavor for uiautomatior tests
    test {
        minSdkVersion 18

dependencies {
    // Instrumentation tests
    androidTestCompile ''
    androidTestCompile ''
    // Set this dependency to build and run UI Automator tests
    androidTestCompile ''