Double Espresso Anyone? ☕️

Espresso Framework - Some Additional Info You Probably Missed

Oren Aviad
AT&T Israel Tech Blog
3 min readApr 21, 2020

--

  1. Overview
  2. Android Test Orchestrator
  3. Espresso Test Recorder
  4. ActivityScenarioRule
  5. How to get current Activity
  6. Combine UIAutomator & Espresso tests
  7. Error: “Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view’s area is displayed to the user

Overview

Testing user interactions within your app ensures that users do not encounter unexpected results or have a poor experience when interacting with your app. If you are an Android developer, Espresso Framework is definitely the right tool for it.

Espresso is targeted at developers who believe that automated testing is an integral part of the development lifecycle. While it can be used for black-box testing, Espresso’s full power is unlocked by those who are familiar with the codebase under test.

In this article, I’ll share with you some useful info I’ve encountered while working with Espresso Framework.

Android Test Orchestrator

Android Test Orchestrator allows you to run each of your app’s tests within its own invocation of Instrumentation

What issues does it solve?

  • Minimal shared state - each test runs in its own Instrumentation instance
  • Crashes isolation - even if one test crashes, it takes down only its own instance of Instrumentation, so the other tests in your suite still run

To enable from Gradle, you should add the following statements to your project’s build.gradle file:

How to Set up Android Test Orchestrator

Espresso Test Recorder

The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app. Espresso Test Recorder then takes the saved recording and automatically generates a corresponding UI test that you can run to test your app

How to configure Espresso Test Recorder

Android Studio Preferences -> Espresso Test Recorder
  • You might want to lower Assertion depth to 1. This way, the added assertions will not rely on child positions to identify the asserted elements and result in much simpler assertions
  • You might want to uncheck Clear app data before/after recording. This way, your app state will not be cleared before/after each recording

To learn more about Espresso Test Recorder, you can read here.

ActivityScenarioRule

This rule is an upgraded version of ActivityTestRule. The previous version will be deprecated and eventually removed from the library in the future.

How to get current Activity

Sometimes when writing a test you need to know what the current activity is being used. In case you are in the activity under test, you can use the following:

However, in case you aren’t, you should use the following:

Combine UIAutomator & Espresso tests

While Espresso framework allows you to test many things inside your application in a simple way, UIAutomator is an automation framework for testing outside of your application, which allows you to perform actions such as:

  • Change the device rotation
  • Press a hardware key, such as “volume up”
  • Press the Back, Home, or Menu buttons
  • Open the notification shade
  • Take a screenshot of the current window

Fortunately, we can combine these two frameworks in a test.

Combine UIAutomator & Espresso tests

Error: “Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view’s area is displayed to the user

In case you got this error, it is because the view you are trying to perform an action on is not fully displayed. To solve it, you should use a custom ViewAction:

Now It’s time to drink my espresso 😜

~ Thanks for reading ~

--

--