Espresso has been one of the most used test automation frameworks for Android app testing. We’ve provided support for it with all our solutions years ago and it’s time to look at how things have evolved and what’s new with Android Espresso testing. You can quickly get started with Android Espresso using Bitbar Public Cloud.

What’s New with Android Espresso

Despite Android Espresso was originally targeted for developers who can build test scripts for their apps, Google recently introduced the Espresso Test Recorder that can be used to quickly and easily create tests for Android apps. People who don’t have a background in programming or even building test scripts can use this tool and with the help of it generate automated user interface tests for their apps.

Test scripts generated by Espresso Test Recorder are executable in Android Studio using emulators and real Android devices that are either connected to a local development environment or resided on cloud service. The cloud service with a rich diversity of different Android devices from all regions, form factors, and different OS versions can be found from Bitbar’s real device cloud.

As Android Espresso aims to improve productivity and make developers work more efficient, it can also provide very effective improvements in mobile app quality. Tests are easy and quick to build, execution of test scripts is definitely the fastest compared to any other Android test automation framework, and the light-weight API takes care of keeping tests understandable, easy to maintain and tweak.

How to Get Started Quickly with Android Espresso

As said, we’ve provided support since the earliest versions of Android Espresso. Each and every version of Espresso has contributed something new to this framework and made test automation easier on a variety of devices simultaneously. If you look for a quick tutorial on how to get started with Android Espresso there are a webinar, ebook, and several blogs available about it.

In addition to the basics of how-to material, we’ve provided some tips and tricks on how to make Espresso yet more suitable for mobile app testing and how to use existing code examples for Espresso tests. The thing to remember that Espresso is just a regular Java code and anyone mastering Java can quickly get up and running with Android Espresso.

In a nutshell, Android Espresso is not but a light-weight API with three components: viewMatchers, viewActions, and viewAssertations. These components are the building blocks of test scripts. The syntax is as follows:

onView(viewMatcher).perform(viewAction).check(viewAssertation);

The comprehensive cheat sheet for Android Espresso is available on the Google Testing site. And here are a few examples of Android Espresso use:

// Example of Espresso use: Sign In for Twitter App on Android onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/sign_in", null, null))) .perform(click()); // Example of Espresso use: Input Text in Edit Field onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/edit", null, null))) .perform((typeText("Testing with Espresso is awesome!")));

Now, from where all that speed comes from? Android Espresso has been said (and it’s a fact) to be the fastest mobile test automation framework. Basically, Android Espresso provides automatic synchronization of test methods and user interface elements shown on the screen. Let’s say test script wants to do an interaction on the user interface (e.g. button click) but the visual element is not available/shown on the screen, the test script will wait until it is.

How to Use Espresso Test Recorder

Android Espresso Test Recorder was introduced in Android Studio 2.2 (preview 3) and at the time of writing this blog, it’s still in beta mode. However, it seems to work relatively well and generates user interface activities robustly and generates the Java code that is instantly executable. And this tool doesn’t require any installations or configurations to get started.

Just select Run and Record Espresso Test and you’ll be asked to select Deployment Target. Now, we always recommend using real Android devices for testing as it will give you all possible details of how the app runs and performs on that device. Naturally, this test can be then deployed easily on other devices and the generated test script is not hardcoded with the hardware details of where a test was originally created/generated and tested on.

Once you have done your test, Android Studio will automatically generate the source code and include in your application’s project.

As an example, the application has a basic login procedure with username (email) and password credential check. First, I type ‘testdroid’ on username field, give a password of ‘password’ and click ‘Sign in or register’. The application notifies that I didn’t give a proper email address first so I’ll do that and click again ‘Sign in or register’. After this, you simply click Complete Recording and the Java code will be generated for your app. Here is an example:

public void loginActivityTest() { ViewInteraction appCompatAutoCompleteTextView = onView( withId(R.id.email)); appCompatAutoCompleteTextView.perform(scrollTo(), click()); ViewInteraction appCompatAutoCompleteTextView2 = onView( withId(R.id.email)); appCompatAutoCompleteTextView2.perform(scrollTo(), replaceText("testdroid"), closeSoftKeyboard()); ViewInteraction appCompatEditText = onView( withId(R.id.password)); appCompatEditText.perform(scrollTo(), replaceText("password"), closeSoftKeyboard()); ViewInteraction appCompatButton = onView( allOf(withId(R.id.email_sign_in_button), withText("Sign in or register"), withParent(allOf(withId(R.id.email_login_form), withParent(withId(R.id.login_form)))))); appCompatButton.perform(scrollTo(), click()); ViewInteraction appCompatAutoCompleteTextView3 = onView( allOf(withId(R.id.email), withText("testdroid"))); appCompatAutoCompleteTextView3.perform(scrollTo(), click()); ViewInteraction appCompatAutoCompleteTextView4 = onView( allOf(withId(R.id.email), withText("testdroid"))); appCompatAutoCompleteTextView4.perform(scrollTo(), replaceText("testdroid@bitbar.com"), closeSoftKeyboard()); ViewInteraction appCompatButton2 = onView( allOf(withId(R.id.email_sign_in_button), withText("Sign in or register"), withParent(allOf(withId(R.id.email_login_form), withParent(withId(R.id.login_form)))))); appCompatButton2.perform(scrollTo(), click()); }

Now, to get your application thoroughly tested across different device variants, we recommend using Bitbar Testing to get as compelling test coverage as it is possible.

You basically need both APKs (application and the test) and user account in the service. Just log in and create an “Android” project for your test – and then follow the test run creation wizard.

You’ll be asked to locate application APK and test APK from your local hard disk and select the device group for a test run. After this, there are some advanced configurations available.

NOTE! For Android Espresso test runs you need to add a proper instrumentation runner for your test run:

android.support.test.runner.AndroidJUnitRunner

Add this in ‘Custom test runner’ section, and click Start.

Android Espresso runs tests quickly but as we always make sure the user experience is the best for cloud users, we clean and reboot devices. This typically takes less than a minute and tests will be executed right after.

Move to the result page of a test run and you’ll get all details of that run with logs, screenshots, performance/memory charts – and now also with recorded video of the test session.

One of the most significant benefits of doing this with our device farm is that you are not limited to the number of devices you can use for simultaneous test runs. Literally, we have hundreds of unique models that can be all assigned for test sessions and Espresso test results are quickly ready.

Happy Espresso Testing folks!