Android Espresso Cheat Sheet
The year is slowly coming to an end, so just the right time to look into what’s new around test automation frameworks.
Espresso by default has many matchers that help you find views that you need to do some checks or interactions with them. Most important ones can be found in the following cheat sheet. Espresso Test Android Cheat Sheet Thanks to Google, testers have a clear cheat sheet that they can refer to while developing test cases with Espresso. The cheat sheet below provides references to most of the instances that come with the Espresso Components discussed earlier. Image Credit: Espresso Framework by Android Developers – Google.
Top on the list of the most used testing tools for Android is still Espresso, so I’ll give a brief overview of developments, but then also introduce some new test automation frameworks.
Here is the section overview:
- What’s new in Android Espresso?
- Kaspresso
- Barista
- BusyBee
- Repeato
- Waldo
What’s new in Android Espresso?
Since Android Espresso is already very well known, I will not go into the basic description of the framework here. Instead, I have compiled a list of the features that have been released in the last few months (Jan 2020 – Nov 2020):
- Add Espresso ViewMatcher APIs for negative conditions
- Allow unregistering Loopers from Espresso IdlingRegistry
- Support specifying junit RunListeners via java.util.ServiceLoader
- Support filtering by prerelease SDKs via SdkSuppress#codeName
- Add truth extensions for Location
- Add truth assertions for Bundle string and parceable arrays.
- Support ActivityOptions in ActivityScenario
- Make activity lifecycle transition timeout configurable
- Add a BundleMatchers.isEmpty() and isEmptyOrNull() methods
- Allow Intents.release without Intents.init
- Add ViewMatchers.isFocused()
- Enhance error messaging for WithIdMatcher
- ViewMatchers.isDisplayingAtLeast() works for views with negative scale.
- Remove spurious wait in waitForAtLeastOneActivityToBeResumed.
- Set correct meta state for ACTION_UP
On top of that there is a great cheat sheet available for download here.
Nothing has changed about the tiresome topic of idling resources. These are still necessary to keep espresso up to date about whether it needs to wait before executing the next step. For example because a background process has not yet been completed.
NestedScrollViews are still not supported either, so an extra implementation is necessary.
Kaspresso
Kaspresso is a test automation framework built on top of Espresso and UIAutomator. With a lot of useful additions. It was developed to improve some of the known weaknesses of espresso (flakiness, no ADB support, difficult to read code).
The Kakau Library was used to ensure better readability.
Let’s take a look how Kakau compares to pure Espresso. Here is a simple Espresso test example:
And here the same test in Kakau DSL syntax:
Because longer tests can become confusing, Kaspresso’s team has extended the Kakau DSL. This way we can also don’t have to care about what is under the hood of our tests, it’s either Espresso or UI Automator.
A full test written with Kaspresso could look like this:
The speed comparison with a conventional UIAutomator (Appium) Test is also remarkable:
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 testing framework also automatically closes system dialogs that sometimes cause a test to fail because they cover important elements of the app.
If you want to know more, the project and functions are really well documented.
Advantages of Kaspresso:
- Greatly improved stability for tests compared to all other test automation frameworks
- Improved speed
- Abstraction and thus better maintainability for tests
- Open Source (110 PullRequests & 887 stars on Github)
Cons of Kaspresso:
- DSL adds a further level of abstraction and requires some time to get to know and familiarize yourself with
- DSL can sometimes lead to confusing code. See an example here.
- Documentation and some issues are partly in Russian
Barista
Barista is a big extension of espresso. The stability (flakiness) is improved with a retry strategy. For certain tests, you can specify how often a test should be repeated if it fails. In addition, like Kaspresso, Barista uses an abstraction applied to PageObjects for UI components and test routines.
A simple example:
Fairly legible right? Obviously, some more work is needed to implement those methods. But still, the structure is very clean and readable:
In case of a feature change in the app you might be able to avoid changing all your 50 tests and instead only change the implementation of a single test method.
Advantages of Barista:
- Improved stability through auto retry and auto scroll
- Good readability thanks to an additional level of abstraction (Page objects)
- Very good documentation, easy to use
- Open source and active community (220 pull requests & 1222 stars on Github)
Disadvantages of Barista:
- Idling resources still have to be implemented manually
BusyBee
BusyBee is also an extension of Espresso. More specifically, a substitute for CountingIdlingResource is used, which has some known difficulties. One of them being debugging: If a test fails because CountingIdlingResource does not signal an idling state, it is often not clear why the exception happened.
Instead of just tracking the number of ongoing processes, BusyBee logs the specific, currently running processes. This is accomplished by tracking a Java object, a character string or an ID.
If a timeout occurs, the list of running processes can be output. This way debugging is getting easier.
Advantages of BusyBee:
- Simple tool that simplifies the handling of tiresome IdlingResources
- Open Source (11 pull requests, 143 stars)
Cons of BusyBee:
- Small tool, no substitute for barista or kaspresso
Repeato
Repeato is a new NoCode test automation framework for Android. Tests can be recorded using a recorder and then played back. More specifically: When recording, small “fingerprints” are recorded in the vicinity of the mouse pointer. On replay, these are used to find the corresponding UI element again.
There is also the possibility of creating “Script Steps” which allow you to execute ADB or other terminal commands.
That’s how you record a test:
https://www.repeato.app/wp-content/uploads/2020/11/record.mp4Afterwards, the recorded steps can then be played back:
https://www.repeato.app/wp-content/uploads/2020/11/replay.mp4Tests created with Repeato are stored in the file system and can also be versioned via Git as well as shared and edited within your team.
There is a free version, but advanced features such as continuous integration support or workspaces are not included.
The tool is particularly helpful as an extension for manual testing: Since developer resources are often scarce, it makes sense to outsource part of the test automation to the QA team.
Advantages of Repeato:
- Very easy to use, can be used by non-developers (QA team)
- Repeato does not need access to the source code of the app
- Installation of the software only takes a few minutes
- Visual reporting dramatically simplifies troubleshooting
Cons of Repeato:
- no open source
- extended version is commercial (49 EUR per month)
Waldo
Waldo is a new cloud service that allows you to record tests using a recorder. The developers have focused on offering a simple interface that also allows non-developers to create tests. The whole thing works, just as with Repeato, without a line of code.
A visual comparison of the original test run (everything worked) with the faulty test run (exception happened) makes debugging a breeze:
Disclaimer: I encountered some difficulties while testing the service:
- The instructions on how to export the build were not visible (empty dialog)
- When starting the build, an error occurred several times – unfortunately that’s when I ran out of patience. TBC…!
Advantages:
- Very clear UI, easy to use
- Great animation, the llama signup is really fun!
Cons:
- Price ($ 1999 USD / month)
- more complex use cases or data driven testing not possible
- Currently no Android support (“Coming soon”)
- The service does not seem to be over beta stage yet
Conclusio
Even with Espresso continuously being improved, there is still enough potential to make test automation more practicable, maintainable and intuitiv. Therefore there are still new but also older alternative test automation frameworks that address these weaknesses. Unfortunately, there is no clear test winner. Each of the frameworks clearly has its strengths and weaknesses.
Computer Vectors by Vecteezy
Tags:
Like this article? there’s more where that came from.
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:
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.
Android Espresso Tutorial
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.
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:
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.
Android Espresso Cheat Sheet Template
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.
Android Espresso Cheat Sheet Excel
Happy Espresso Testing folks!