App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Getting Started with Espresso – Android UI Automation

Getting Started with Espresso – Android UI Automation

By Akshay Pai, Community Contributor -

The Android operating system dominates the mobile phone market with a massive 71% market share. Users spend more time on their smartphones, doing various activities such as Gaming, Messaging, Video Calling, Reading, Entertainment, Shopping, Meetings, and Working, which has led to businesses developing Android Apps.

Android Market Share

However, with more Android Apps in the market, users have more options to choose from, which is why businesses have to deliver top-notch apps for user retention and engagement. UI Testing plays a major role in delivering a great user experience. Espresso Testing checks your Android App by providing Android UI Automation.

Using Espresso Testing Framework, QAs can ensure that their Android App delivers a user experience that aligns with the business requirements. This article provides a beginner’s guide to using Espresso for Testing Android Apps.

What is the Espresso Testing Framework?

Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework. Espresso is one of the popular Android App Testing Frameworks, that is widely used by QAs. It allows black-box testing, at the same time, allows QAs to test fragments and individual components during development cycles.

Espresso is highly robust. It allows developers to test both Android native views as well as hybrid web-views. Test cases using this framework can be written in Java or Kotlin, ensuring no new skill development is required to use it.

Using this framework, testers will be able to leverage a plethora of features. Synchronized test executions, intent validations, and capabilities to run recipes are some of its prominent features.

Understanding Espresso Testing in Android

Source: Android Developers

Espresso automation involves understanding the paradigm in which it has to be used. As a developer, one needs to first understand that it’s purely an Android app UI testing framework. This explains why the Espresso Android framework is built without tight coupling to Android APIs such as getView() and getCurrentActivity().

The first step while starting with Espresso is to assume that you are a user. Create test cases by first identifying which UI element has to be tested and then initiating some action on it (basically interacting with it). This makes sense as it causes testers to write automation test cases that mimic user interactions with the UI.

Espresso’s API Components

Here are the four API components of Espresso that lay the foundation of Espresso Testing:

1. Espresso

This is the starting point for all test cases. This component provides entry points or methods to start the interaction with the app’s view. Each app’s view has two components.

First is the part of the view that belongs to the app. Testers will be able to interact with it by using the onView() and onData() methods. The second part of the view consists of the components that are provided by the OS ( home screen button, back button, etc). Espresso also provides APIs to interact with non-app components. For example, the pressBack() method initiates going back.

2. ViewMatchers

Views are always in a hierarchy called the View Hierarchy. For the test cases to navigate the view hierarchy, ViewMatchers are used. Technically, they are a collection of objects, and they implement the Matcher interface. Testers will be able to pass one or more of these objects to the OnView() method provided by Espresso Component.

3. ViewActions

These components define the action that has to be performed on any given View. Espresso allows testers to send more than one ViewAction as a collection to the Interaction method. An example of a view action would be the click() method which helps a test script click on a given View’s UI component.

4. ViewAssertions

Assertions complete a test case. They are the components that check if the test has passed or failed. In Espresso Android, the ViewAssertions can be passed to the Check() method from the ViewInteraction package. Matches assertion is the most commonly used check which verifies the final state of the view with what is expected.

Sample Test Case for Espresso Android

Based on the components discussed above, here is how one can write a test case with Espresso using:

1. Java


2. Kotlin


Code Explanation

Here’s what both the Java and Kotin test case above does.

  1. Firstly, it tries to find a view with a certain ID and a uniquely identifiable feature. In this case, the script is saying the text “Hello!” on the view is a unique feature.
  2. Next, the script performs two actions on that view. It first types “Hello!” on a text box that is present, and then it clicks on a button that exists.
  3. Finally, it calls the assertion matches() to check if the view shows “Hello!” once the click is performed. The result will be a successful test case if the view has the desired text on it. Otherwise, the test case fails.

Espresso Test Android Cheat Sheet

Thanks to Google, testers have a clear cheat sheet that they can refer to while developing test cases with Espresso. The cheat sheet below provides references to most of the instances that come with the Espresso Components discussed earlier.

Espresso Android Testing Tutorial

Why use Espresso Instead of Appium?

Espresso and Appium complement each other. Both are test frameworks used for automation but they differ in where they are applicable. Appium helps with testing how well an app responds to various scenarios and how it handles various user flows. Espresso on the other hand helps you test an Android app’s UI and UI component behavior.

Read more about Appium vs Espresso to thoroughly understand the technical differences.

Despite using Appium, it is highly recommended to use Espresso as well to ensure that there is end to end coverage of test cases.

Run Espresso Tests on Real Devices

This Android Espresso tutorial strives to offer core information and starting points for action with the Espresso Testing Framework. The initial learning curve might seem a bit steep, which is true for any sort of automation testing. However, with time, it will pay off, and you will be saving a lot of time and money.

It is recommended for the SDETs and QA teams to run Espresso Tests on real devices and take real user conditions into account while testing. A cloud-based real device tool like BrowserStack Automate provides access to all the latest and legacy real Android Devices such as Samsung Galaxy Devices, Google Pixel, One Plus, etc., to run your Automation tests and achieve accurate test results for better quality. Check out the entire List of Devices below.

Try Espresso Testing on Real Devices

List of Android Devices for Espresso testing on BrowserStack App Automate


Automation Testing Espresso Mobile App Testing

Featured Articles

Top Android Automation Testing Tools & Framework

What is Espresso Testing? How does it work?

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.