How I learned to automate Android apps with Android Espresso

Photo by Andrew Neel on Unsplash

How I learned to automate Android apps with Android Espresso

·

3 min read

As a mobile app developer, previously working with React Native for cross-platform applications and now as a native Android developer, I was looking into automated testing of my Android app with either Java or Kotlin.

The reason for automated testing is simple: prevent regression bugs from sneaking into the application while refactoring parts of the product.

With previous knowledge of other end-to-end testing solutions in Javascript (Mocha), I figured it would be good to learn how to do this with Android.

Turns out Android offers Android Espresso, which offers functional and end-to-end testing, supported out of the box for all Android devices.

Setting up Android Espresso

There's not much setup needed, as it's available on all devices. It's a feature provided by Google and documented on the Android Developer Documentation website.

Writing the first Android Espresso test

After reading the documentation, and the Cheat Sheet I started out with writing a basic test for my main activity.

The syntax is pretty easy to read and write, it's very descriptive in an almost BDD way. I simply started out with the following snippet:

Espresso.onView(Matchers.allOf(ViewMatchers.withText("enter text") , ViewMatchers.isDisplayed()));

This basic snippet tries to match a specific element on the current screen (Activity), and check if it is currently visible.

Once you know how to locate elements, you can start interacting with them.

Tapping an element is very straightforward with Espresso, you can use:

onView(withId(R.id.home).perform(click())

The perform function accepts various ViewActions, including:

  • click
  • doubleClick
  • longClick

Bringing it all together, you can create your first Espresso test which inputs a text in a specific element and verifies whether the input was successful.

@RunWith(AndroidJUnit4.class)
public class SampleTest {

    /**
     * Use {@link ActivityScenarioRule} to create and launch the activity under test.
     */
    @Rule
    public ActivityScenarioRule<MainActivity> activityScenarioRule =
            new ActivityScenarioRule<MainActivity>(MainActivity.class);


    @Test
    public void ensureInputWorks() {
        onView(withId(R.id.input1)).perform(click()).perform(typeText("this is a test"));

        // Check that the text was changed.
        onView(withId(R.id.input1)).check(matches(withText("this is a test")));
    }
}

You can run this specific test with Android Studio and receive the test results inside Android Studio. You'll need to make sure either an Android Emulator or physical Android device is attached to your computer.

Running Android Espresso tests in the Cloud

Once I had most test cases ready, I figured it would be beneficial to run these on a variety of Android devices. Since I only own 1 Android device, I looked for a solution where I could run the test on multiple devices.

TestingBot offers a cloud-based solution with multiple different Android devices (and Android emulators) which are capable of running Android Espresso tests in the cloud.

The idea is that you upload both your .apk file and the .apk file containing the Espresso tests. Then select the specific devices and run the tests. You'll get the test results, with a video.

Conclusion

Android Espresso is a great utility to quickly create and run functional tests for native Android applications. Definitely worth giving a try, it really increases developer productivity in the long run.