The vast majority of Android developers strive to write code that is both reliable and concise. They accomplish this by automating the testing of their Android applications using the Espresso framework. This post will discuss the fundamentals of automating Android testing with Espresso, including an explanation of Espresso’s features and benefits. We will also show you how to run tests in Espresso on Sauce Labs and explore some integrations you can use.
Espresso, an Android automation testing framework, was created by Google to perform functional testing on Android programs. Espresso is a powerful open-source testing tool that can be used to test an Android application's user interface while it is being developed in the Android Studio IDE.
Espresso testing is critical because it enables the creation of reliable UI tests for Android applications and provides excellent support for running tests on both actual devices and emulators. Espresso is popular due to its fast test cycle, ease of setup, and suitability for black box testing (due to its inability to access classes outside your application).
Espresso testing has numerous benefits for both Android developers and automation testing engineers. We’ll discuss them further below.
Espresso is a comprehensive framework for automated UI testing for Android apps. It enables faster test execution through automatic UI synchronization.
Because it is based on Java/Kotlin, Espresso provides an intuitive way for Android developers to work.
Espresso speeds up developer feedback by allowing tests to be compiled into a separate APK.
Espresso is well known for producing consistent test results and for its ability to reduce test flakiness.
Espresso is simple to set up because it works well within the Android Studio IDE.
Users can easily maintain and customize Android UI tests using Espresso APIs.
Espresso enables developers to monitor the device's internals for finer control and also test activities outside the application.
Testing is a necessary step in the app development process. You can validate your app's correctness, functional behavior, and usability before releasing it publicly by running tests against it on a regular basis. While you could use manual testing methods, using an automated testing framework like Espresso allows for faster test execution, scalability, and quicker actionable feedback about your application throughout the development process.
Espresso offers a wide range of notable features, including the following:
Synchronization capabilities – The Espresso framework automatically syncs with user interface elements and test actions. It has idling resources, which aid in accurately validating asynchronous actions when testing Android apps.
Scalability and flexibility – Espresso is highly flexible and scalable, in addition to being simple and easy to learn.
Support for Android intents – Espresso uses an intents extension that allows for the validation and stubbing of intents sent out by the application being tested. An intent is essentially a passive data structure that contains an abstract description of a task to be completed.
Espresso also has a separate module for testing the Android WebView component.
The main components of the Espresso API include the following:
Espresso – The Espresso component serves as a point of entry for interactions with application views (via onView() and onData()). It also exposes APIs that aren't always associated with any app view, such as pressBack ().
ViewMatchers – This is a collection of objects that implement the Matcher <? super View> interface to identify and locate an app view. You pass one or more viewMatchers to the onView() method if you want to locate a view within the current view hierarchy.
ViewActions – This is a group of objects that enable us to perform actions on the identified views. Typically, they are passed to the ViewInteraction.perform() method, such as click ().
ViewAssertions – These are the objects that can be passed to the ViewInteraction.check() method. ViewAssertions allows us to assert the state of a view by determining whether the state of the currently selected view matches the expected state.
You can expand the scope of mobile test automation and increase developer productivity by running automated Espresso test suites on Android apps with Sauce Labs’s real device cloud. Using Sauce Labs for Android Espresso testing has the following additional advantages:
Sauce Labs offers excellent support for the Espresso framework. This allows developers and test engineers to run reliable automated UI tests on Android apps.
By utilizing the vast selection of Android emulators and actual devices available in the Sauce Labs cloud, you can enhance test automation coverage and execute Espresso tests at scale at any stage of your development cycle.
Users can analyze test executions and share test results by combining Espresso and the Sauce Labs cloud. This ensures that users can get developer feedback faster and fix bugs earlier in the development lifecycle.
Executing tests in a single platform (like Sauce Labs) with the framework of your choice allows you to work with the tools and in the language and IDE (integrated development environment) with which you are already familiar.
Sauce Labs runs Espresso tests using their framework-independent test orchestrator, saucectl, which is based on one or more configuration files that tell saucectl how to run your tests.
The results are published in your Sauce Labs account and can be compared across contexts and frameworks in a single view.
A Sauce Labs account (log in or sign up for a free trial license).
Your Sauce Labs username and access key.
Your Espresso apps and files for real devices and/or emulators.
Once you have that set up, follow the steps below.
Begin by running the following command to grant the saucectl CLI access to your local project:
sudo sh -c 'curl -L https://saucelabs.github.io/saucectl/install | bash -s -- -b /usr/local/bin
To obtain the saucectl directory structure and example files to use as templates, clone or download the Espresso example repo that corresponds to your project.
Generate a credentials file for saucectl to use to authenticate your CLI commands.
Configure saucectl.
At the prompts, enter your Sauce Labs SAUCE USERNAME and SAUCE ACCESS KEY.
saucectl creates a credentials.yml file in your home directory's .sauce folder.
Modify the config.yml file inside the root directory of your demo repo according to the configuration documentation for Espresso.
Use the saucectl CLI run command to execute your tests:
saucectl run
To learn how to run Espresso tests on the Sauce Labs cloud, click here for a video tutorial.
The code snippet below shows a sample UI test written in Espresso and Junit4 using Java. Android Studio generates tests in the src/androidTest/java/com.example.package/ directory by default.
The sample test below is used to assert that the app view contains the text "Hello World."
1@RunWith(AndroidJUnit4.class)23@LargeTest45public class HelloWorldEspressoTest {678@Rule910public ActivityScenarioRule<MainActivity> activityRule =1112new ActivityScenarioRule<>(MainActivity.class);131415@Test1617public void listGoesOverTheFold() {1819onView(withText("Hello world!")).check(matches(isDisplayed()));2021}2223}
In this section, we will compare the three most popular mobile app automation testing tools: Espresso, Appium, and XCUITest. Since we are already familiar with Espresso, let’s explore Appium and XCUITest before comparing them.
Appium is a popular solution for testing native, hybrid, or mobile apps on iOS, Android, and Windows desktop platforms. It enables testers and developers to create automated tests for mobile applications, allowing them to deliver high-quality software more quickly and safely.
Appium is completely open source, with cross-platform support and the ability to develop tests using a variety of tools in any WebDriver-compatible language, including Java, Objective-C, and JavaScript.
XCUITest is a framework for creating user interface (UI) tests. Apple released XCUITest in 2015 as a significant update to improve testing capabilities. It is based on XCTest, Apple Xcode IDE's integrated test framework. It does not have access to the core methods of an app, and it runs in a separate process from the main iOS app. XCUITests can be written in Swift or Objective-C.
Espresso is written in Java, whereas Appium can be written in any language.
Appium was developed as an open-source project, while Espresso was natively created by Google.
Appium is better suited for black box testing, while Espresso is better for white box testing.
Espresso only offers native Android UI testing, but Appium provides cross-platform testing.
Espresso requires minimal setup, but Appium necessitates a bit more.
Espresso is an Android test automation framework, whereas XCUITest is an iOS test automation framework. Espresso and XCUITest both perform white box testing, and both produce very reliable test results.
The main distinction between Appium and XCUITest is that Appium works across platforms, whereas XCUITest is only available for iOS.
The table below highlights some of the feature considerations you should look for when deciding which test framework is best for you.
Attribute | Espresso | Appium | XCUITest |
Language Supported
| Java and Kotlin | Any | Swift and Objective-C |
Used By | Android developers | QA teams | iOS developers |
App Supported | APK | APK and IPA | IPA |
Test Type | Instrumented black and white box testing | Non-instrumented black box testing | Instrumented white boxing |
Speed | Fastest at t speed | Slower at 8t speed | Faster at 2t speed |
Continuous Integration
| Easiest for CI | Harder for CI | Hardest to implement CI |
Easy to Set Up? | Easiest to set up | Hardest to set up | Not as easy to set up as Espresso |
Support for Visual Testing | Does not support visual testing | Supports visual testing | Does not support visual testing |
Test Stability | Reliable tests | Flaky tests | Reliable tests |
Object Locators | Uses ID – R file | Uses an external object locator – X-path | Uses ID |
In this guide, we covered the Espresso framework's essentials for automated UI testing on Android applications. While Espresso has many advantages, its efficiency could be improved – especially by increasing test coverage and visibility. Users need a comprehensive platform that supports a variety of real devices, emulators, and simulators to get the most out of UI testing, thereby ensuring increased coverage, developer productivity, speed, and app release confidence.
Sign up for a free Sauce Labs trial and experience the benefits of mobile and UI testing in the Sauce Labs cloud.