Espresso Tutorial: A Detailed Guide To Espresso Testing

Espresso is a native open-source framework to automate UI testing of Android applications.

Espresso

What is Espresso?

“Use Espresso to write concise, beautiful, and reliable Android UI tests” – Google.

Developed by Google, Espresso is an Android automation testing framework and has gained popularity due to its high performance. Using Espresso, you can create very simple and straightforward tests without worrying about the application's infrastructure. It is also open-source so that developers can customize it. According to research from the University of Oulu, Espresso is extremely fast and has the shortest test execution time and fallibility.

You can perform Espresso testing in both Java and Kotlin languages. Espresso also synchronizes your test actions with application UI. It lets you test native and hybrid views of Android applications. Furthermore, Espresso enables you to conduct black-box and component testing of applications and provides testing annotations and assertions. It also comes integrated with Google’s Android Studio.

Why is Espresso testing important?

If you are working on an Android project, validating your projects across various cycles like build, development, and testing can be cumbersome. Below are the pointers that demand the need for Android testing of the application user interface -

  • Every Android build requires validation after a code commit.
  • Dependency on remote servers can slow down test execution
  • Testing Android apps requires the latest APIs from Android OS that support new platform features.
  • Android app testing should be performed on Android Emulator online and real devices.

Using the Espresso framework, you can write reliable Android UI tests and achieve fast test execution speed while reducing test flakiness.

Espresso provides an extensive set of features like.

  • Synchronization Capabilities
  • Android Intents
  • Synchronization Capabilities: The Espresso framework ensures that the UI or application you are testing has already started before your test begins. It lets you ensure that the background applications have been completed. Espresso also has idling resources - an example of asynchronous actions whose results ultimately affect operations in a UI test. Registering idle resources with Espresso can accurately check these asynchronous actions when testing Android apps.
  • Android Intents: Android Intents is a mechanism that lets your application communicate with the Android System and third-party applications. With Espresso, you can manage the behavior of Android Intents to stub them out to provide pre-defined responses. Tests become more robust when there is less dependence on changes to external apps introduced with each release. Stubbed or Mocked Intents will also reduce the number of times your automated test fails due to a glitch in an external application.

Feature of Espresso

When it comes to Android testing, Espresso provides loads of features to help test automation engineers and developers fasten their Android app testing process. Besides Synchronization capabilities and Android Intents, below are the following additional features of the Espresso framework.

  • It automatically syncs with user-interface elements and test actions.
  • Espresso is highly flexible and scalable. It has simple and lightweight APIs that is easier to learn and maintain.
  • It offers support for Unit testing and Black-box testing.
  • It provides a separate module to perform Android WebView testing of UI components and to test and mock Android Intents.
  • It provides a separate module to perform Android WebView testing of UI components and to test and mock Android Intents.
  • Using Espresso, you can test Java and JUnit native applications.

Benefits of Espresso framework

By using Espresso for Android automation testing, you can reap numerous benefits. Some of them are as follows -

  • Using Espresso lets you build a test suite as a standalone APK file. You can install the APK file alongside the application under test and quickly run it on your device or APK Emulator Online.
  • It enables developers to achieve faster feedback on code changes and debug and fix issues faster.
  • Espresso doesn’t use any server like Selenium RemoteWebDriver for interaction. Instead, it runs alongside the application and provides test results instantly.
  • It provides a synchronized execution technique due to the high stability of the test cycle. Additionally, it has a mechanism that confirms that the Element or Object is shown on the screen before proceeding. This prevents test execution from failing due to "objects not detected" and other issues.
  • Test suites for Espresso are easier to develop, making Espresso app development easier. Espresso can also be set up easily using Android Studio.
  • Espresso also supports JUnit 4 framework.
  • It is simple to use as it is built on Java and Kotlin.
  • Android developers can easily monitor the device’s internals for better control.

In the next section of this Espresso tutorial, we will look at how Espresso works on Android.

How does Espresso work?

It is crucial to understand the test scenario before automating your apps with Espresso. Testers should think about what a user can do while interacting with Android applications, i.e., finding UI elements and interacting with them.

Espresso doesn’t allow direct access to activities and views of the applications as it can result in test flakiness. This is why you won’t see the methods like getView() and getCurrentActivity(). To access views securely, you can create your subclasses of ViewAction and ViewAssertion.

The working of the Espresso framework is broken down into the following steps -

  • Locate a specific View within the entire View hierarchy.
  • Perform some actions on the View that you just identified.
  • Make some assertions on the View. There is no need to perform any actions to do assertions. Instead, just find a View and make assertions.
Espresso Working

Components of Espresso's API

In the previous section of this Espresso tutorial, we focused on the working of the Espresso framework. Now let’s look at the major APIs that form the component of the Espresso framework.

There are four types of Espresso’s API.

  • Espresso: The Espresso components provide entry points to initiate the interaction with the application view. You can interact with the app’s view using the OnView() and OnData() methods. It also provides APIs not specific to any app’s view, like pressBack().
  • ViewMatchers: These matchers are used to identify and find a view. They are collections of objects that implement the Matcher interface. If you want to locate a view within the current view hierarchy, you can use these objects to the onView() method.
  • ViewActions: After locating a view, you need to perform some actions with it. ViewActions are a collection of objects that you can pass to the ViewInteraction. perform() method like click().
  • ViewAssertions: These are the collection objects you can pass to the ViewInteraction.check() method. As soon as you find a view, you need to check whether its state matches what you expected. The final set of Espresso's API components can be used to perform assertions against the state of views during a test.

How to create UI tests for Espresso?

This section of Espresso tutorial talks about writing UI test cases in Java and Kotlin based on the above discussed Espresso’s API Components.

Java

@Before
  onView(withId(R.id.my_view),withText("Hello! Ready to perform Espresso testing"))
  .perform(typeText("Hello! Ready to perform Espresso testing"),click())
  .check(matches(withText("Hello! Ready to perform Espresso testing")));

Kotlin

onView(withId(R.id.my_view),withText("Hello! Ready to perform Espresso testing"))
.perform(typeText("Hello! Ready to perform Espresso testing"),click())
.check(matches(withText("Hello! Ready to perform Espresso testing")))

The above test cases are written in Java, and Kotlin identifies a view with a specific ID and a unique feature. Here, the test script says, "Hello! Ready to perform Espresso testing" which is a unique feature. On that view, the test script performs two actions.

First, it types “Hello! Ready to perform Espresso testing” in a text box and then clicks on a button. Once the button clicks, it checks if the view displays “Hello! Ready to perform Espresso testing” by calling assertion matches(). After passing the test, you will see the desired text in the view.

Espresso vs Appium: Which one to choose?

In this section of Espresso tutorial, we will look at how Espresso differs from Appium.

Both Espresso and Appium synchronize with each other. However, their application areas differ. Appium is used for end-to-end testing of mobile applications, whereas Espresso is used for UI testing of Android applications.

Let’s check out the key difference between Espresso and Appium.

EspressoAppium
It involves UI testing of Android applications.It involves automated app testing of mobile applications (Android, iOS, and Windows).
It can test Android applications only.It can test both mobile (Android, iOS, and Windows) and desktop applications.
It’s easier to set up.It is tedious to set up as it required the installation of third-party programs.
It supports Java and Kotlin only.It supports multiple programming languages like Java, JavaScript, Python, PHP, etc.

Limitations of Espresso framework

Despite numerous benefits, there are some disadvantages of the Espresso framework.

  • It can only test user interfaces. Therefore, Espresso has a limited test scope.
  • It can’t perform iOS automation testing of mobile applications.
  • It only supports two programming languages - Java and Kotlin. So has languages and technology stack limitations.
  • In order to use Espresso, you need access to the app’s source code.
  • Testers should have a fundamental knowledge of Android testing to use the Espresso framework.

Espresso Android Cheat Sheet

If you want to kick start your app test automation using Espresso, check out this handy Espresso testing Cheat Sheet for Android.

Espresso Working

How do I get started with Espresso?

Espresso is a top-notch framework to incorporate into your test automation strategy. Therefore, it's crucial to execute Espresso testing efficiently. Setting up expensive Android device labs can be expensive and hard to scale up. This demands the need for a real device cloud platform like LambdaTest that eliminates the hassle of setting up an in-house device lab. Leveraging LambdaTest’s online device farm, developers and testers can perform Espresso testing on real Android devices from anywhere at any time.

About LambdaTest

LambdaTest is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run manual and automated testing of web and mobile apps across 3000+ browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve a faster go-to-market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs.

Getting Started with Espresso testing on LambdaTest

To start Espresso testing on LambdaTest, you can head over to our Espresso testing guide for a step-by-step procedure for running your first Espresso test script on a cloud Espresso Grid. So you don’t have to worry about the challenges with Espresso infrastructure as we will provide you with a cloud Espresso Grid with zero downtime.

Want to know a fun fact? Your first 100 Espresso test minutes are on us with just a free sign-up on LambdaTest. With a lifetime subscription to LambdaTest, you can also avail benefit from cross browser testing, manual app testing, visual UI testing, responsive testing, and more.

Frequently Asked Questions

What is Espresso for mobile testing?
Espresso is a native open-source framework for automated UI testing of Android applications. It is part of the Android SDK and is used by developers to create native mobile apps.
Is Espresso used for black-box testing?
Espresso is neither a black box nor a white box testing framework. Rather, it's a grey box testing framework. Since it has the same access to the application's internals as a white box (unit/integration tests), it's named a grey box (black box + white box).
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie