Android Automation Testing: Getting Started with Espresso

Maksim Akifev
4 min readJun 17, 2018

--

How to automate Android application testing? How to setup Espresso Framework and write robust tests using Java? Read on to learn.

Introduction to Android Automation testing tools

There are different approaches how to automate user interface testing and integration for Android native applications. When choosing test automation framework we need to make sure that:

  • Framework is easy to set up and maintain
  • Documentation and support available
  • Framework is scalable and getting updates

A couple of tools such as Appium, Calabash and Ranorex meet these criteria. In this tutorial, we’ll be taking an advantage of Espresso Framework.

Getting started with Espresso

Espresso is a robust Test Automation Framework developed by Google allows developing automated tests for Android Applications. It provides the functionality to simulate real user interaction with the screen and adding test assertions, as well can be integrated with different kinds of libraries for tests performance enhancements.

Initial configuration of Espresso Framework is minimal and can be easy integrated to existing Android Project.

Tests can be executed on physical devices and emulators. Espresso Framework brings a native support of parallel execution, which allows us to reduce test execution when running compatibility tests.

Espresso Framework is integrated with Test Recorder which allows starting writing tests with minimal coding knowledge.

Writing first Espresso Test with UI Recorder

Before we can write our first test we need to clone application source code repository and open it in Android Studio. The next step will be to set up simulator or real android device testing. In this tutorial, we’ll be using Android Virtual Device. You can learn how to configure it here. When you have it completed, we are ready to write the first test:

  • Android Studio > Run > Record Espresso Test
  • Select device for testing
  • Execute test on the device
  • Add assertion to verify expected behavior
  • Save Test Class

When you have successfully recorded your test, accept adding dependencies by Android Studio. This will automatically setup Espresso for project. AndroidTest folder will content generated test class.

Unfortunately using test recorder is not always a best approach for writing reliable tests, cause recorder don’t capture system alert interactions and generates not optimized code, but it’s an easy way to get started with Android Automation testing using Espresso and helps us to set up project with required libraries.

Running tests and generating test report

You can run test class or single tests manually from Android Studio interface, just open the test class and click desired action button.

Running tests in Android Studio

Another approach is to run tests from command line interface. This command will execute instrumented tests on all connected Android devices:
./gradlew connectedAndroidTest

Generated test report HTML and XML report will be stored in path_to_your_project/module_name/build/outputs/androidTest-results/connected/ directory.

Locating elements with UIAutomatorViewer

The better approach to inspect elements and determine the locators is to use UIAutomatorViewer. If you have Android SDK installed on your machine, it should be in $ANDROID_HOME/tools/bin directory. Launch it by navigating to that directory and executing ./uiautomatorviewer in terminal.

resource_id and text are the most common type of locators, that you’re going to use. The complete element locator can be written as following:

Matcher contactInListLocator = withId(R.id.contact_name)

You can also specify a multiple element for a locator if you have nested elements or need to be more precise:

Matcher contactInListLocator = allOf(withId(com.simplemobiletools.contacts.R.id.contact_name), withText("TestName TestSurname"))

Espresso cheat sheet

Espresso API exposes availability to perform almost any possible user interaction with the screen as well as specifying certain element conditions.

Tapping an element

onView(contactInListLocator).perform(click());

Clearing textfield

onView(onView(surnameTextFieldLocator)).perform(clearText(“Testname”));

Typing text

onView(onView(firtsNameTextFieldLocator)).perform(typeText(“Testname”));

Add sleep statement

Thread.sleep(500);

Adding Assertion

onView(contactInListLocator).check(matches(withText(testContactName)));

You can learn more about Espresso matchers, view actions and view assertions in Cheat Sheet below.

Source: https://developer.android.com/training/testing/espresso/cheat-sheet

Granting runtime permissions

The best way to grant application runtime permissions is to use GrantPermissionRule . As a first step, we need to integrate required libraries in your project:

  • Open app/build.gradle file
  • Add library to your list of dependencies
  • Sync Project with Gradle files
dependencies {    // Other dependencies ...

androidTestImplementation "com.android.support.test:runner:1.0.0"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.0"
}

When we have integrated the library, add Junit Test Rule to handle runtime permissions:

@Rule
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS) .grant(Manifest.permission.WRITE_CONTACTS);

What’s next:

  • Improving tests with Screen Objects Pattern
  • Mocking Networking calls
  • Running tests in parallel in CI server

Conclusion

In this tutorial we have learned how to set up Espresso for your Android Studio project and how to write basic tests with test reports.

Using Espresso is great approach to automate user interface and integration tests for native Android application. It will allow starting writing robust and easy setup automated tests for your project. Complete Test Framework setup example with Application Source Code is available on GitHub, I highly recommend checking it out to learn more details in context.

Stay tuned for more tutorials and Happy Testing :)

About the Author

Maksim Akifev is a highly skilled Quality Assurance Engineer with wide experience in Test Automation, Security and Penetration Testing who helps Companies in various industries such as Finance, Healthcare, and Cyber Security to build reliable and secure Products.

--

--

Maksim Akifev

Helping companies to build reliable and secure products