Android Espresso With Examples

In this tutorial, we’ll be discussing Espresso Testing Framework for Testing the UI of Android Applications. We’ll be creating a basic login application with a few espresso tests.

Android Espresso

We’ve already discussed Android Unit Testing before.

Testing is an important area for building any product.

It helps us in:

  • Detecting bugs/cases not covered by our code.
  • Keep manual user testing to minimal. No need to test the earlier features again and again manually.
  • Building robust applications with end cases covered.

Testing in Android majorly involves two types:

  • Unit Tests
  • Instrumentation Tests

Espresso comes under the second type. It is used to do automated UI testing by writing short and concise Android UI tests.

Let’s look at the components of the Espresso Instrumentation Testing Framework.

ViewMatchers, ViewActions, ViewAssertions

  • ViewMatchers – allows us to find a view in the view hierarchy.
  • ViewActions – allows us to perform automated actions on the view. Such as clicks etc.
  • ViewAssertions – allows us to assert the state of the view.

A basic skeleton code of an Espresso Test is:

Inside the onView we look for the id of the View.

It can be done using methods like:

  • withId() – Pass the unique id
  • withText() – Pass the text of the view. It searches for a view with the specified text.

Inside perform we pass the action to be done on the View. Example:

  • click() – Clicks the view passed in onView.
  • typeText() – Pass the string to be entered in the View. This is especially used in EditText.
  • replaceText() – Replaces the current text with the string that’s passed.
  • closeSoftKeyboard() – Dismisses the keyboard.

Inside check we assert the states mainly using methods:

We can test the view hierarchy using Espresso tests as well.For example we can Position View Assertions in the following ways:

Other methods for position assertions are:


Hamcrest Matchers are very powerful matchers.
The following examples show that:

The last one checks whether the whole TextView is a link.

In the following section, we’ll create an android application and write our first espresso tests.

Our android application would contain a basic login form with a username, phone number and confirm number fields. On pressing the Login Button, if the phone number fields don’t match we will show a Toast message.
If everything is fine, we’ll great the user with a Hello.

Getting Started With Espresso Tests

Add the following dependencies in the build.gradle file:

Add the following in the defaultConfig block :

AndroidJUnitRunner is the instrumentation runner. It is the entry point into running the Android Tests.

Following is how our app’s build.gradle file looks :

android-expresso-test-build-gradle (1)

Make sure you disable the Animations in your Android device before running Espresso tests.
Otherwise, the default screen animations may interfere with the tests. You can disable them from the Developer Options as shown below:

android-expresso-developer-options (1)

Project Structure

android-retrofit-rxjava-timer-interval-project-structure (1)


The code for the activity_main.xml layout is given below:

The code for the class is given below:

The code for the strings.xml file is given below:

Writing Espresso Tests

Espresso tests are written inside src | androidTest folder files.
By default, is created with a default test.

Let’s write one test inside it:

@Rule annotation is used to launch the MainActivity.
Inside loginClickedSuccess we wrote a successful login test case.

Now let’s write two more tests in which we will test whether the Toast is shown correctly or not.

Add the following methods inside the above class:

The last line is used to check whether Toast is displayed with the correct string or not.

To run the espresso test. Right click on the and press run to start the automated test on the emulator.

Following is the output of the application in action:

android espresso test output

Following is the output in the console while the above automated UI test was running:

android espresso test console output

Espresso Test Recording

You can record Espresso tests manually as well:

It will automatically create a new file with the espresso tests containing the manual clicks and actions you did in the device.

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply