Did you know you can test your Android apps at scale on real mobile devices without having to write any automated testing code? Espresso Test Recorder is a UI testing tool that lets you create tests for your app without having to write any test code. AWS Device Farm is an app testing service that lets you test your app on hundreds of real Android devices in the cloud.
By the end of this tutorial, you’ll lean how to:
- Create UI Tests for your Android app using Espresso Test Recorder
- Generate an APK that contains your Espresso tests
- Upload your app and tests to Device Farm and execute them at scale on hundreds of real devices.
To get started, you will need an Android app that you would will like to test. For this tutorial, I developed a sample Android app in Kotlin using the instructions here. If you are building the app using those instructions, you should end up with this:
Espresso tests consist of two primary components: UI interactions and assertions on View elements. UI interactions include tap and type actions that a person may use to interact with your app. Assertions verify the existence or contents of visual elements on the screen.
The Espresso Test Recorder will generate an APK with all your tests in it. When you are testing on a real device, you will need to upload this test APK to the device along with the APK of the app you would like to test. If you wish to run the tests on a simulator you can simply right-click on the generated test file, and select Run Tests.
If you have your own project, open it in Android Studio. If you followed the tutorial then you can skip this step.
To record your tests, select
Run > Record Espresso Tests from the Android Studio menu.
Next, pick the device you wish to run your tests on and then press
OK. Android studio will build your app and load it onto the chosen device and present a dialog for recording your tests.
Once your app is built and you are shown the Record Your Test dialog, go ahead and interact with your app.
Espresso Test Recorder will automatically record your interactions as
touch events. When you run the tests later, the Espresso test will execute the events in the same order in which they were recorded.
Once you have a few interactions recorded, click the
Complete Recording button.
We won't cover adding assertions here, but if you want to add some assertions as well, you can follow this tutorial and then come back.
If you have not added the Espresso dependencies to your app, a Missing Espresso dependencies dialog appears when you try to save your tests. Click
Yes to automatically add the dependencies to your
In order to upload your tests to Device Farm, you will need to build APKs for both the app and your tests.
Make sure to create a new configuration for your tests or an APK won't be generated. For instructions on how to set up the proper configuration, go here.
Once the build has completed and you have your APKs, navigate to Device Farm in the AWS console. In the Device Farm console, click the create a new project button.
After configuring your new project, select Create a new run. Here you will be presented with a wizard that will allow you to configure your tests and pick your test devices.
First you will need to upload your app APK. Next, on the configuration tab, choose
Instrumentation for test type and upload your test APK, then click
Device Farm supports hundreds of device configurations allowing you to run your app against exactly the type of devices you need to. To specify what devices you want to run your tests on you can either use a pre-configured pool from AWS like “Top Devices”, or you can create a new device pool. Feel free to choose either.
Once you have selected a device pool, click
For Device State stick with the defaults unless your app depends on specific data or apps being available on the device. Once again go ahead and click
Before running your tests make sure to review all the settings and set a max timeout for your tests, then click
Run. This will start the tests and create the run, you can view the run in real time by clicking on the new run in the list of available runs.
That's all there is to it! You can now add automated testing to your Android apps without having to write a line of code. Pair that with Device Farm and you can deploy your apps knowing they work across all of your supported device types!
Are you confused by the rewrite vs refactor debate? In this post, I'm going simplify things for you and help you decide if you should rewrite, refactor, or do something else to improve the health of your software system.