Android Espresso – 4 tools you should be using
Introduction
If you ever tried automating user interface tests for Android applications, you know it can be challenging. But we should remember that writing good and effective tests should be fun at the same time! Knowing the right tools can save you a lot of time and effort so I have prepared the list of my favorites.
Android Espresso is my first choice when it comes to testing framework. It is easy to set up, fast, reliable and has a fluent API. On the other hand, it is not perfect yet. Espresso does not provide some obvious solutions even with typical operations that happen during writing automation tests, such as:
- Setting up application into a testable state
- Setting application locale
- Granting runtime permissions for the application
- Turning on/off the network or Wi-Fi data
- Turning off device’s animations
- Mocking and simulating different API states
- Parallel tests execution on multiple devices
- Generating readable and meaningful test reports
Have your ever experienced that? Fortunately, by using few additional libraries and shortcuts, this can all become much simpler. Let’s take a look at some easy-to-get solutions you may use.
Test Butler
Test Butler tool provides a couple of features that will make your Android testing more reliable and flexible. Test Butler is a two-part project. It includes an Android library that your test code can depend on, as well as a companion Android apk file that must be installed on Android emulator before test suite execution.We use Test Butler especially to:
- Grant runtime permissions
String accessCoarseLocation = Manifest.permission.ACCESS_COARSE_LOCATION;
String accessFineLocation = Manifest.permission.ACCESS_FINE_LOCATION;
TestButler.grantPermission(context, accessCoarseLocation);
TestButler.grantPermission(context, accessFineLocation);
- Simulate running the app in specific language
TestButler.setLocale("pl", "PL", context);
- Turn on/off network or Wi-Fi data
TestButler.setGsmState(false);
TestButler.setWifiState(false);
Simple, right? Just a single line of code is enough to perform each setup. But – it comes with a price – Test Butler works only on Android simulators.
RESTMock
RESTMocklibrary lets us to mock API HTTP responses in Android Espresso tests. It can be really helpful when it comes to writing reliable and maintainable automation tests. RESTMock helps to:
- Speed up test execution by skipping communication between API and application under the test
RESTMockServer
.whenPOST(pathContains("/auth"))
.thenReturnFile("login_response.json");
- Simulate different API conditions such as HTTP 5xx error or incorrect responses
RESTMockServer
.whenGET(pathContains("/userdata"))
.thenReturnFile(500, "userdata_response.json");
- Simulate API responses delays. It is especially handy in testing how application under the test behaves on API timeouts or in bad network conditions
RESTMockServer
.whenGET(pathContains("/userdata"))
.thenReturnFile("userdata_response.json")
.delay(TimeUnit.SECONDS, 15);
Easy preferences
A lot of Android applications use Shared Preferences to store application data, settings or cache. Easy Preference library lets us to simplify the interaction with Shared Preferences on Android and helps us to avoid writing boilerplate code. One of the application, to which I wrote the tests, had a user field form, where user data has been cached. Setting up already prefilled form in @Before
method lets Android Espresso save a lot of time before each test’s execution:
@Before
public void setUserData()
{
EasyPreference.with(context)
.addString("firstName", "John")
.addString("lastName", "Doe")
.addString("city", "Warsaw")
.save();
}
@After
public void clearSharedPreferences() {
EasyPreference.with(context)
.clearAll()
.save();
}
Spoon
We have already set up all great libraries and written tests. Now it’s time to execute them and take a look at the test report. Thanks to the Spoon tool we are able to run Android Espresso tests on multiple devices simultaneously. Spoon will run on all targets which are visible to adb devices
.
Once all tests are completed, a static HTML summary is generated with detailed information about each device and test.
In addition to simply running Espresso tests, Spoon has the ability to snap screenshots at key points during your tests which are then included in the output. This allows for visual inspection of test executions across different devices.
Taking screenshots requires including the spoon-client JAR in your instrumentation app. In your tests, call the screenshot method with a human-readable tag.
Spoon.screenshot(activity, "after_login");
Conclusion
Automating user interface testing on Android applications is not a piece of cake. That’s why, you should always look for solutions that make your testing easier and faster. The presented set of tools can certainly save you a lot of time and make your Android Espresso tests more efficient.P.S. Learn more on the differences and advantages of Appium and Espresso on upcoming webinar where we are going to compare them: CLICK HERE TO REGISTER