Android Espresso Cheat Sheet

Posted : admin On 1/2/2022

Overview

Espresso is a UI test framework (part of the Android Testing Support Library) that allows you to create automated UI tests for your Android app. Espresso tests run on actual device or emulator (they are instrumentation based tests) and behave as if an actual user is using the app (i.e. if a particular view is off screen, the test won't be able to interact with it).

Espresso's simple and extensible API, automatic synchronization of test actions with the UI of the app under test, and rich failure information make it a great choice for UI testing. In many circles Espresso is considered to be a full replacement for Robotium (see this stack overflow post that compares Robotium to Espresso).

Android espresso cheat sheet 2019

Android Studio Setup

Espresso Cheat Sheet Master updated According to this fragment of your post: When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is 'wrong answer'. It means that you hadn't set disabled your next question button, so Espresso passes this test.

New to Android phones and need a little help? Get the most use out of your Android with some useful tips and handy advice: Learn how to peruse the home screen, familiarize yourself with common phone icons and touchscreen gestures, discover some tricks, and know where to go for help. Android Interview Questions - Your Cheat Sheet For Android Interview. Prepared and maintained by Amit Shekhar who is having experience of taking interviews of many Android developers and cracking interviews of top companies. A complete guide for learning Android Development - Check here Contents. Core Android; Android Libraries; Android. Kaspresso chooses a very sophisticated strategy (compared to Espresso or Barista) to improve the stability (flakiness) of tests: Android’s event queue is monitored and commands are only sent when Android is in a calm state and there is a low likeliness that UI commands will be skipped because of lack of computation resources. In addition, the.

Android espresso cheat sheet excel

Android Espresso Cheat Sheet Free

There are several steps needed to setup Espresso with Android Studio:

  1. First, let's change to the Project perspective in the Project Window. This will show us a full view of everything contained in the project. The default setting (the Android perspective) hides certain folders:

  2. Make sure you have an app/src/androidTest/java folder. This is the default location for instrumentation tests.

  3. You'll also need to make sure you have the Android Support Repository version 15+ installed.

  4. It's recommended to turn off system animations on the device or emulator we will be using. Since Espresso is a UI testing framework, system animations can introduce flakiness in our tests. Under Settings => Developer options disable the following 3 settings and restart the device:

  5. Finally, we need to pull in the Espresso dependencies and set the test runner in our app build.gradle:

    That's all the setup needed. Now let's move on to writing some actual tests.

Creating a Simple Espresso Test

The code below shows a simple Espresso test that enters some text into an EditText and then verifies the text entered. It's based off the standard new project template which has a single MainActivity that contains a TextView with the text 'Hello world!'.

  1. Add an EditText to MainActivity that has id = R.id.etInput.
  2. Create a new class MainActivityInstrumentationTest inside of the default instrumentation tests directory (src/androidTest/java). The best practice is to mimic the same package structure with your tests as your product code. This has the benefit of giving your tests access to package-private fields in your product code. For this example, we'll be creating MainActivityInstrumentationTest at src/androidTest/java/com.codepath.espressodemo.MainActivityInstrumentationTest.

When writing Espresso tests, you'll be using a lot of static imports. This makes the code easier to read, but can make it more difficult to understand when you are first learning Espresso. Let's dive into the parts of the above test:

  • Espresso – This is the entry point. Mostly of the time you'll be using onView(...) to specify you want to interact with a view.
  • ViewMatchers – This is how we find views. ViewMatchers contains a collection of hamcrest matchers that allow you to find specific views in your view hierarchy. Above, we've used withId(R.id.etInput) to specify we are looking for an EditText with id = R.id.etInput.
  • ViewActions – This is how we interact with views. Above we've used the typeText(...) method to type Hello into our EditText.
  • ViewAssertions – This is our validation. We use ViewAssertions to validate specific properties of views. Most of the time you'll be using ViewAssertions that are powered by ViewMatchers underneath. In our example above the withText(...) method is actually returning a ViewMatcher which we've converted into a ViewAssertion using the matches(...) method.

The standard pattern for an Espresso test is to find a view (ViewMatchers), do something to that view (ViewActions), and then validate some view properties (ViewAssertions). There's a handy cheat sheet that's a great reference to see what's available in each of these classes.

Running Espresso Tests

There are 2 ways to run your tests:

  1. Run a single test through Android Studio:

    • Right click on the test class and select Run:
    • Note: If you are presented with two options as in the diagram below, make sure to select the first one (designating to use the Gradle Test Runner instead of the JUnit Test Runner). Android Studio will cache your selection for future runs.
    • View the results in the Console output. You may need to enable Show Passed as in the diagram below to see the full results.
  2. Run all the tests through Gradle:

    • Open the Gradle window and find connectedDebugAndroidTest under Tasks => verification.
    • Right click and select Run
  • This will generate an html test result report at app/build/reports/androidTests/connected/index.html

  • Note: You can also run the tests on the command line using: ./gradlew connectedDebugAndroidTest

Other Espresso Test Scenarios

Interacting with a ListView

ListView is an AdapterView. AdapterViews present a problem when doing UI testing. Since an AdapterView doesn't load all of it's items upfront (only as the user scrolls through the items), AdapterView's don't work well with onView(...) since the particular view might not be part of the view hierarchy yet.

Fortunately, Espresso provides an onData(...) entry point that makes sure to load the AdapterView item before performing any operations on it.

Let's run through an example of how you might interact with a ListView in Espresso. Our ListView will be a simple list of text strings.

Espresso

Alternately, if we know the position of the particular item, we can directly specify the position instead of using a data Matcher to find it:

Interacting with a RecyclerView

Unfortunately, RecyclerView is not an AdapterView so we can't use onData(...) for a RecyclerView, but Espresso does support RecyclerView in the android.support.test.espresso.contrib package.

Let's first pull the package into our app build.gradle:

Now we can use RecyclerViewActions to interact with our RecyclerView:

Android Espresso Cheat Sheets

You can see all the supported RecyclerViewActions (including how to interact with views inside of the RecyclerView item) here.

Stubbing out the Camera

A limitation of Espresso is that it's only able to interact with the single app under test. This means that if your app launches another app, your Espresso test code won't be able to interact with the other app at all. When we think about scenarios where an app will launch the camera (i.e. another app) to take a photo and then return the resulting image to the original app, this becomes problematic.

Fortunately, Espresso provides the android.support.test.espresso.intent package to help us mock out the intent.

Let's assume we have an activity inside of our app that lets the user take a photo:

Now assume that we want to test this activity and have an actual image 'returned' to our app from the Camera. This is where Espresso-Intents can help. If we didn't have Espresso-Intents, our app would launch the Camera, but have no way of getting any resulting image back from it.

To start using the intent package, let's first add it to our app build.gradle:

Then, let's build an actual test that shows how to stub out the Camera intent:

References

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

Android Cheat Sheet

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.

Android Espresso Cheat Sheet

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:

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

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:

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.

Android Espresso Cheat Sheet

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 Espresso Cheat Sheet Pdf

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.

SheetAndroid Espresso Cheat Sheet

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!