If you're an Android developer, you're probably familiar with Google's Espresso UI test automation framework. Espresso is newer and more lightweight compared to UIAutomator. It supports 2.2 Froyo and up, and it’s also a fluent API with powerful Hamcrest (https://github.com/hamcrest) integration that helps make code more readable and extensible.
Espresso is useful for Hybrid app testing, or webview heavy testing, but it does not have access to system integration tests, therefore, it is recommended on developer.android.com for the “Testing UI for a single App” test.
The Espresso testing framework is an instrumentation-based API and works with the AndroidJUnitRunner test runner. Using Espresso benefits the client because it bundles the tests on the client’s actual device. The advantage here is that it runs on the device, which runs faster, reduces network traffic, and has control over the classes and controller while Appium has multiple layers.
Now, instead of running Espresso tests on a single device at a time, you can run the test on multiple Android devices and in parallel with Quantum Framework on Perfecto Cloud devices.
Before I describe how easily you can set up to run Espresso tests on multiple devices and in parallel, let me explain about the Quantum Automation Framework (QAF). QAF is an open-source test automation framework for web and mobile test authoring, execution and analysis. The framework enables ease of environment set-up to access the Perfecto Cloud Lab with user authentication and driver management. It allows you to create a test script in Behavior-Driven development (BDD) for easy-to-use English phrased steps, and it will generate Perfecto DigitalZoom reporting of the test execution for quick analysis and drill down to the root cause of failure. The QAF Starter-Kit is available on GitHub.
The following describes a high-level step of how you can run the Espresso UI Tests on multiple devices in parallel by using an open-source Quantum project.
1) Compile and Build Android APK Files for Espresso UI Tests
With Android Studio, you need to create the Espresso UI Test Script and validate it. It can be run from the Android Studio IDE.
Build the two APK files, which will be “Tested Application File” and “Espresso Test File.”
For my example, I have a project called SimpleNotesEspresso, and after creating the Espresso UI test script, I then build the APK files as shown below by using a command line:
Once the APK files are built, then the next step is to upload these files onto your Perfecto-hosted Cloud Lab repository.
2) Set Up and Use the Quantum Starter-Kit Project
Download and install the QAF start project from GitLab.
I am using IntelliJ Community Edition as my IDE for the Quantum Project, and I have installed Perfecto Continuous Quality Lab plugin for my IDE tool to access cloud devices.
Once installation is completed, the first thing you need to do is make edits to the
application.propertiesfile under the resources folder, and make changes to the user credential information and hosted-cloud domain name, as shown:
Next, you will need to create a custom steps file, and place it under the steps folder (src/main/java/com.quantum/steps).
- Right-click on the steps folder -> New -> File, and then enter a filename and save it as EspressoStepsDefs.java.
- Then add the following code as shown below. Make sure you are referencing to the proper APK files that you had created from the above instructions.
Now, we need to create a scenario file and place it under the scenario folder (src/main/resources/scenarios).
- Right-click on the scenarios folder -> New -> File, and then enter a filename and save it as espresso.feature.
- Then add the following code as shown below:
Finally, we need to create a TestNG configuration file to allow us to run the test script on multiple devices in parallel. Therefore, we need to create this file and place it under the config folder (src/main/resources/config).
- Right-click on the config folder -> New -> File, and then enter a filename and save it as testng_espressoDemo.xml (or whatever name makes sense).
- Alternatively, you can just copy the testng_appium.xml file and then make edits to fit your devices’ needs.
- In the example shown below, I will be setting up to run three different Android devices and enabling them to run in parallel. Notice, the device selection is set using a wildcard that indicates to select any available Android Galaxy S-type phone on the CQ Lab, however, if you want to select a specific device then you would need to change the parameter capabilities from using a "model" to "deviceName" and set the device value to a specific Unique Device Identifier (UDID) number.
As shown in the image below, I will be using the wildcard for Galaxy ‘S’ phones, and each of my test suite names are unique. (i.e., Android Simple Notes Espresso Test1).
3) Get Espresso UI Tests Running on Multiple Devices and in Parallel
Right-click on testng_EspressoDemo.xml file -> and select “Run…” from the menu items.
To view which devices are running espresso UI tests, click on the Perfecto Dashboard pane and it will show devices with testing in progress.
While using Android Studio to build and run the Espresso UI test script in a single device mode, if you would like to increase your testing coverage by running on different Android devices and OS versions in parallel to increase testing velocity, I would recommend using the Quantum starter-kit project. It has all the open-source components to help increase device test coverage and speed up testing velocity. With that said, it means that you are using Espresso UI Testing framework in combination with a TestNG testing framework built in a Quantum project to easily get multiple devices running in parallel.