How to Do Android UI Testing With Automation
It's important to get Android UI testing right. Here we cover Android UI automation and how to achieve it.
What's the Android UI Testing Framework?
Espresso is Google's Android UI test automation framework. It's more lightweight compared to UIAutomator and it’s newer. It supports 2.2 Froyo and up, and it’s also a fluent API with powerful 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. You should use it for "Testing UI for a Single App."
Espresso is an instrumentation-based API and works with the AndroidJUnitRunner test runner. Using it 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. (Appium has multiple layers.)
Why Add Automation to UI Testing on Android
Now, instead of running 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 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. 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.
How to Do Android UI Testing With Quantum
The following describes a high-level step of how you can run the Android UI Tests on multiple devices in parallel by using an open-source Quantum project.
1. Compile and Build Android APK Files For UI Tests
With Android Studio, you would need to create the Espresso UI Test Script and validate it. It can be run from the Android Studio IDE
Then build the two APK files, which will be “Tested Application File” and “Espresso Test File."
For my example, I have a project called SimpleNotesEspresso. After creating the Android UI test script, then I build the APK files as shown below by using a command line:
In terminal, type $>gradleassembleAndroidTestassembleDebug.
This compiles and builds the two APK files (app-mock-debug.apk and app-mock-debug-androidTest.apk )
Once the APK files are build, 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 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.properties file under the resources folder. And then 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 make 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 set it up to run three different Android devices and enable 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. But 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 on the image below, I will be using the wildcard for Galaxy ‘S’ phones. Each of my test suite names are unique. (i.e., Android Simple Notes Espresso Test1).
3. Get Android UI Tests Running on Multiple Devices and In Parallel
Right-click on testng_EspressoDemo.xml file -> and select “Run…” from menu item
To view which devices are running Android UI tests:
- Click on Perfecto Dashboard pane and it will shows devices testing in progress.
Android UI Automation With Perfecto
If you want increase your test coverage by running on different Android devices and OS versions in parallel to increase testing velocity, you'll need to add automation to Android UI testing. 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 both Espresso framework for Android UI testing in combination with a TestNG testing framework build in a Quantum project for easily getting multiple devices running in parallel. To see how I implement end-to-end workflow of Espresso with Quantum, watch the video below.
Get Started With Perfecto
Perfecto integrates with Espresso to automate Android UI testing.
With Perfecto, you'll get Android UI automation and:
- Accelerate test execution.
- Scale as you grow.
- Boost test coverage with a mobile device cloud.
See for yourself how Perfecto can help you. Get started with a free 14-day trial.