Getting Started with Espresso – Android UI Automation
By Akshay Pai, Community Contributor - June 5, 2020
The Android operating system dominates the mobile phone market with a massive 75% market share. From attending meetings to playing games, to collaborative artwork, it can be done on an Android device. Naturally, creating and testing Android applications is quite the rage these days.
Naturally, the development of Android has also ramped up. In fact, 3739 apps are added to Play Store every day. This means that developing an Android app that will get noticed, downloaded, and used consistently has become fairly challenging. App developers need to use the best possible tools at their disposal to give their apps a fighting chance in the digital market. One of these tools that all Android developers and automation test engineers need to use is the Espresso automation framework.
This article will provide a beginner’s guide to using Espresso, helping them get familiar with a framework that is mandatory in order to build Android applications correctly.
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. It allows both black-box testing as well as testing of 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.
This tutorial delves into why an automation test framework needs to be used, when it should be used and how to get started with Espresso.
Should You Use an Automation Testing Framework?
Android App Automation Testing reduces costs and saves a lot of time during development cycles. The initial investment in terms of both cost and time is high for test automation. However, studies and analysis over the years have proven that the ROI on Automation Testing is always higher.
The analysis above shows how automated testing reduces cost over multiple development cycles. Another analysis from Abastrata shows how they were able to reduce the cost of testing per hour from $78 to $17.5.
So, keeping these metrics in mind, there is no doubt that testers should use an Automation Test Framework.
Understanding Espresso Android
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
Now that we are familiar with Espresso’s design model, let’s look at the four components that make up its high-level components.
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 to initiate going back.
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.
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.
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:
onView(withId(R.id.my_view),withText("Hello!")) .perform(typeText("Hello"),click()) .check(matches(withText("Hello!")));
onView(withId(R.id.my_view),withText("Hello!")) .perform(typeText("Hello"),click()) .check(matches(withText("Hello!")))
Here’s what both the Java and Kotin test case above does.
- 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.
- 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.
- 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.
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.
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.
SDETs and QA teams can use the Espresso Framework on a cloud-based real device lab like BrowserStack to run their Automation tests.