Back to Resources

Blog

Posted February 14, 2023

Getting Started with Espresso

Espresso provides faster test execution, scalability, and actionable feedback when used for testing Android applications.

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.

What Is Espresso?

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).

Benefits of Using Espresso for Automated Testing

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.

Espresso Features

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.

Components of the Espresso API

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.

Why Perform Espresso Testing with Sauce Labs?

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:

Testing Android apps reliably

Sauce Labs offers excellent support for the Espresso framework. This allows developers and test engineers to run reliable automated UI tests on Android apps.

Scaling Espresso Automation

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.

Debugging Faster with Analytics

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.

Maximizing Developer Productivity

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.

How to Run Tests in Espresso on Sauce Labs

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.

Prerequisites – what you'll need:

Once you have that set up, follow the steps below.

1. Install saucectl​

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

2. Check the demo repositories

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.

3. Connect your Sauce Labs account

  • 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.

4. Configure your test parameters

Modify the config.yml file inside the root directory of your demo repo according to the configuration documentation for Espresso.

5. Run your tests

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. 

UI Test Example in Espresso

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)
2
3
@LargeTest
4
5
public class HelloWorldEspressoTest {
6
7
8
@Rule
9
10
public ActivityScenarioRule<MainActivity> activityRule =
11
12
new ActivityScenarioRule<>(MainActivity.class);
13
14
15
@Test
16
17
public void listGoesOverTheFold() {
18
19
onView(withText("Hello world!")).check(matches(isDisplayed()));
20
21
}
22
23
}

Espresso vs. Appium vs. XCUITest 

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.

What Is Appium?  

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.

What Is XCUITest?

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.

Appium vs. Espresso 

  • 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 vs. XCUITest

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.

XCUITest vs. Appium

The main distinction between Appium and XCUITest is that Appium works across platforms, whereas XCUITest is only available for iOS.

Full Comparison: Espresso vs. Appium vs. XCUITest

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

Start UI Testing with Sauce Labs 

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. 

Published:
Feb 14, 2023
Topics
Share this post
Copy Share Link

Need to test right now? Get started free.

Ship code that behaves exactly as it should.

© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.