The Android operating system dominates the mobile phone market with an unbelievable 71% share. Users are spending more time on their smartphones on various activities such as gaming, messaging, video calling, reading, entertainment, shopping, meetings and work, which has led companies to develop Android apps.

As there are more Android apps on the market, we have more choices, so companies need to provide top-notch apps to keep and engage users. UI testing plays a significant role in providing a great user experience.

Espresso testing controls the Android app by providing UI automation for Android. Using the Espresso test framework, a QA tester can ensure that their Android app delivers a user experience that aligns with business requirements.

What is Espresso framework?

Espresso is a testing framework that helps developers write automated test cases for user interface (UI) testing. It was developed by Google and aims to provide a simple but powerful tool.

Espresso is one of the popular Android app testing frameworks that is widely used by software testers. It enables black box testing while allowing QA to test fragments and individual components during development cycles.

The Espresso framework is part of AndroidX and is based on UI Automator (a framework for automated application testing), which is part of the Android SDK. Espresso testing is used to test user interaction with the application’s user interface.

Espresso allows developers to test native Android views as well as hybrid web views. Test cases using this framework can be written in Java or Kotlin.

Understanding Espresso testing in Android

The first step in getting started with Espresso is to put yourself in the user’s shoes. Create test cases by determining which UI element to test and then initiating an action on it (basically interacting with it). This makes sense because it causes testers to write automated test cases that mimic user interactions with the user interface.

Espresso framework API components

  1. Espresso

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

The first is the part of the view that belongs to the application. Testers will be able to interact with it using the onView() and onData() methods. The second part of the view consists of the components provided by the operating system (home screen button, back button and others). Espresso also provides an API to interact with components that are not part of the application. For example, the pressBack() method initiates a rollback.

  1. ViewMatchers

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

  1. ViewActions

These components define the action 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 could be the click() method, which helps the test script to click on a UI component of a given View.

  1. ViewAssertions

Assertions complement the test case. These are the components that control whether the test passes or fails. In Espresso Android, ViewAssertions can be passed to the Check() method of the ViewInteraction package. Assertion of conformance is the most commonly used check that verifies the final state of the view with what is expected.

Test Espresso Android Cheat Sheet

Thanks to Google, testers have a clear spreadsheet to refer to when creating test cases using Espresso. The cheat sheet below contains links to most of the cases that are part of the Espresso components we discussed earlier. You can download the cheatsheet here.

espresso framework cheatsheet
Source : https://android.github.io/android-test/downloads/espresso-cheat-sheet-2.1.0

Espresso framework features

Some of the main features supported by Espresso are:

  • a very simple API that is easy to learn.
  • highly scalable and flexible.
  • a separate module for testing the Android WebView component.
  • a separate module for authentication as well as for emulating Android Intents (Intent allows you to trigger an action in another application by describing the action you want to perform in the Intent object, for example “view a map” or “take a picture”).
  • automatic synchronization between the application and the tests.

Espresso framework advantages

  • Easy to set up and use.
  • It is tightly integrated with the Android SDK tools.
  • It has excellent support for running tests on real devices and emulators.
  • It is powerful and can be used to test all kinds of Android apps.
  • It is open source and well documented.
  • Google is constantly improving it.
  • Supports JUnit4.
  • UI automation suitable for writing black box tests.
  • Stable and fast framework.
  • Compact API.
  • Being Java/Kotlin based, Espresso provides an intuitive way of working for Android developers.
  • Espresso speeds up developer feedback by allowing you to compile tests into a separate APK.

Espresso framework disadvantages

  • Espresso supports only Java and JUnit programming languages.
  • Espresso is not always able to properly simulate user interaction with an application that uses multiple threads.
  • May have problems testing applications that use animations.
  • Espresso may have trouble testing applications that use unusual inputs, such as inputs from external devices (if an application uses input from a camera, Espresso may have trouble verifying that the application is processing images correctly).
  • Espresso is for testing the user interface only. Given the scope of what you have to test, this can be limiting.
  • To test using Espresso, you must have access to the source code of the application.
  • To use Espresso, you need to have a basic understanding of Android app testing.

Types of espresso framework automation testing

Espresso offers two types of testing for android apps, both are briefly explained below:

Functional testing

This testing tests the functionality of the application and verify that it works as expected.

Functional tests can be used to verify that the application responds correctly to user input, displays the correct data, and behaves as you expect. They can also be used to test the flow of the application and ensure that all parts are working properly.

Functional tests are essential because they help ensure that the application is reliable and error-free. By regularly performing functional tests, you can catch and fix potential problems before they cause serious damage to your application.

User Interface Testing

This testing is to test the user interface of your application and check that it looks and functions correctly. It can be used to check the layout of the application, verify the appearance of text and icons, and test the functionality of buttons and other UI elements.

In this class, you can write tests to check the layout of your activity, verify the appearance of the app, and test the functionality and other elements. You can also use it to test the flow of the application and ensure that all parts are working properly.

However, there are other frameworks available for testing mobile devices, let’s take a look at their comparison:

Espresso vs Appium

  • Both frameworks are designed for automated testing of mobile applications.
  • Both frameworks are open-source.
  • Both frameworks are considered reliable.
  • Espresso supports testing native apps on Android, while Appium supports testing native as well as hybrid and web apps on Android and iOS.
  • Espresso is considered easier to use than Appium.
  • Espresso is considered faster than Appium.
  • Appium is considered more flexible than Espresso.
  • Espresso is integrated with Android Studio, while Appium is integrated with various IDEs such as Eclipse, IntelliJ IDEA and Visual Studio Code.

Espresso vs XCUITest

  • Both frameworks are designed for automation testing of mobile applications.
  • Espresso supports native app testing on Android, while XCUITest supports native app testing on iOS.
  • Espresso and XCUITest are both considered easy to use.
  • Espresso and XCUITest are considered fast and reliable.
  • XCUITest is considered more flexible than Espresso.
  • Espresso is integrated with Android Studio, while XCUITest is integrated with Xcode.
  • Both frameworks are open-source.

Espresso vs Selendroid

  • Both frameworks are designed for automated testing of mobile applications.
  • Both frameworks are open-source.
  • Both frameworks are considered reliable.
  • Espresso supports native app testing on Android, while Selendroid supports native app testing on Android.
  • Espresso is considered easier to use than Selendroid.
  • Espresso is considered faster than Selendroid.
  • Selendroid is considered more flexible than Espresso.
  • Espresso is integrated with Android Studio, while Selendroid can be integrated with various IDEs such as Eclipse, IntelliJ IDEA and Visual Studio Code.

Espresso vs Robotium

  • Both frameworks are designed for automated testing of mobile applications on Android.
  • Both frameworks are open-source and free.
  • Espresso only supports the native app on Android, while Robotium supports native, hybrid and web apps on Android.
  • Espresso is considered easier to use than Robotium.
  • Espresso is considered faster than Robotium.
  • Both platforms are considered reliable.

If you speak German and are an IT tester or IT automation tester, take a look at our employee benefits and respond to our job offers!

About the author

Michaela Kojnoková

Agile Test Engineer

Po štúdiu informatiky na ŽU a TUKE som sa najviac ponorila do oblasti automatizácie testovania. Okrem toho sa venujem tvorbe webov, databázam, dátovej analytike, umelej inteligencii a strojovému učeniu. Mám rada cestovanie, šport a najviac si užívam čas strávený v prírode s mojimi blízkymi. LinkedIn

Let us know about you