Quick Guide: Doing Espresso Tests on Perfecto Devices

Quick Guide: Doing Espresso Tests on Perfecto Devices

//Quick Guide: Doing Espresso Tests on Perfecto Devices

Espresso is an open-source framework from Google and an instrumentation-based API that works with the AndroidJUnitRunner test runner. Its main function is that it allows you to execute UI tests on Android apps.

After instrumenting your app, the tool provides an easy way to access the objects and execute actions and validations. Here’s a quick guide to setting up Espresso and writing Espresso tests on devices in Perfecto’s Continuous Quality Lab.

Set up Espresso

To add an Espresso test to your project, you should:

1. Install it by adding the lib to the gradle file:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'

2. Instrument the app by adding this line to your gradle file:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Building the app with the following configuration will open the Espresso API and you can start writing your tests.

How to write Espresso tests

Espresso commands sync automatically with the UI and wait for the elements to appear on the screen.

The test scenario will consist of the following steps:

  • onView(object identifier) — Wait for the element to appear in the UI
  • Perform — Execute actions in the element
  • Check — Validation

This video shows how to execute the Espresso command on a Perfecto device and how the results are presented in a report.

The test below opens a sample native Calculator app, inputs numeric values and then validates the results. See how it appears on a device underneath the code sample below.

onView(withText("Perfecto Calc:")).check(matches(isDisplayed()));
onView(withId(R.id.results)).check(matches(withText("10.0")))   onView(withId(R.id.number)).perform(typeText("20"),closeSoftKeyboard());
Calc app -- A device in the Perfecto system while the test is running.

Calc app — A device in the Perfecto system while the test is running.

Why Perfecto?

  • The Perfecto cloud lets you execute your existing Espresso tests on any real devices with any Android version.
  • The Perfecto system generates rich reports that are included in the test video where you can get more details on your test.
  • Visual validation – The Espresso test framework only supports native OS objects and therefore cannot validate the visual aspects of the objects as they appear on the screen.

However, Perfecto allows you to add visual validation to your test scripts and avoid the following issue:

The same app with device rotated. Only visual validation can find the bug above.

The same app with device rotated. Only visual validation can find the bug above.

How to start

In order to execute the Espresso test from the command line, you should use the adb Shell command:

adb shell am instrument -w 

The “adb shell” command requires specific permission, so please contact me at [email protected] for more details.


Uzi joined Perfecto Mobile seven years ago after a fifteen year career as a software developer and manager at IDF, Netrialty, Comverse and Sundisk. Over the past seven years, Uzi has helped grow Perfecto by managing expanding R&D teams and leading sales engineering teams. His fields of expertise include agile methodologies, mobile application testing, automation tools, and defining customer projects and on-boarding.

Leave A Comment