This post is from the Experitest blog and has not been updated since the original publish date.
How to Get Started with Android Espresso
In the past, development and testing were two isolated processes. Development cycles followed a linear-sequential flow. This required any new phase in the dev process to begin only after the previous phase was complete. With the advent of agile, DevOps, and Shift Left approaches, that scenario has completely changed. Today, development is closely tied to testing. Testers affect the testing process right from the beginning of the SDLC. The introduction of DevOps practices has ensured increased coordination. The Shift Left approach transforms the role of test engineers within high-paced dev environments. Testing early and often means that testers can no longer remain passive observers in the system. It has led some of the largest companies in the world to create their own development platforms like Android Espresso.
In line with these practices and approaches, we have an extensive ecosystem of (codeless) automation tools, CI systems, and testing frameworks today. Espresso is one such tool that allows us to create UI tests without writing any test code. In application design and development, the UI factor ought not to be ignored. After all, end users are meant to use every application. They expect reliable performance and a seamless experience all the way. Google's Espresso is there to help developers ensures that their apps' UI is fully functional, user-friendly and bug-free.
What is Espresso?
Espresso is a native testing automation framework for Android. It's part of the Android Support Repository and helps write concise, beautiful, and reliable user interface tests for Android applications. The framework's intention is to test a single application but can also test across applications. In short, you can use Espresso to help you detect errors that slipped through in the development cycle. Ensure your applications provide a reliable and seamless experience to the end users.
Espresso vs. Appium
How to get started with Espresso?
The important thing is that you need to be familiar with Android Studio if you are running an Espresso test. Being familiar with the Android environment will save you almost half the efforts that would spend on learning the ropes. Nevertheless, we have covered everything in our webinar as well as in this blog post to help you get up to speed with Espresso.
Before starting your UI test with Espresso, you need to install Espresso and set up your test environment. If you are not sure how to do it, help from the Android Developers Guide.
Now, get your application ready and upload it on Github for it to be accessed by Android Studio. With that step complete, follow the steps provided below:
Download the application code from the GitHub repository and configure it within Android Studio.
Launch the app on an emulator within Android Studio:
Creating your test on a local emulator is a lot easier within Espresso. Unlike what we had in the past, Android emulators today perform quite effectively as virtualization technology powers them instead of simulations. Emulators are faster, more efficient and reliable than they used to be.
To launch the emulator, locate the play (run test) icon at the top section of the interface (on the right side). Click on "run test," select your emulator option from the box, and then click OK. Your emulator should launch now.
Record the test
On the top menu section, locate "Run" -> "Record Espresso Test" -> Select the device you want to run the recording on. -> Click OK.
This will prompt some background actions. For example running Gradle Build, killing the active application, installing APK file, and relaunching the app. Once the background process is complete, an empty dialogue box to record your test will appear.
Everything you do during the recording process will show up on the dialogue box. In the demo case, we clicked on the text fields for username and password and provided the required input before logging in. As you can see, the dialogue box is showing every interaction we have made on the screen, including every taping and text typing. After logging into your app, you can also add an assertion for the tool to learn the UI and reproduce it later. Once the recording finishes and you have added the assertion, click "OK".
After you are done recording the test and have saved it, you can now look through the code to see if there are any anomalies that should be investigated. You may want to eliminate the recording parts that are redundant or used repeatedly (like the selected code strings on the screenshot).
Execute the test
Once you save the recording, you will land on an interface with a list of tests on the left panel (under the app you are testing). All you need to do is right-click on them one by one and hit "Run Tests in [your test app]" item from the given options.
Running a test on an emulator within Espresso is not the purpose in and of itself. There are many reasons: For one, tests within Espresso execute in tandem and so they take quite a bit of time when you need to run them at a scale. For another, these tests are getting executed on an emulator and not on real devices. So you're never sure the performance you get on real devices would be the same as on the emulator. For yet another, the reporting system within Espresso is not as robust and detailed as that by the Experitest Reporting system.
To execute our Android Espresso test on real devices and at scale, we will need to connect it all with a cloud QA environment such as Experitest's Digital Assurance Lab. We will also need a CI system, such as Jenkins, integrated to ensure that any subsequent changes to the application codes go through the testing pipeline automatedly. So that we don't have to go through the process all over again.
Assemble the test and connect with a cloud environment.
At this stage, you will have to upload the APK and Application files within Espresso to the Experitest QA cloud through Postman and utilizing the REST API. To do that, follow the steps given below:
On the extreme right side and towards the top of the screen, click on "Gradle" -> ":app" -> "build." Under the build folder, find "assembleDebug" and "assembleAndroidTest" files. Right click on each, and hit "Run 'Android:app [the file name]."
It's about done. Let's head on to the integration part now.
Step 6:Executing at Scale with SeeTest Digital Assurance Lab
To trigger the test execution on the cloud, we will need Postman and a REST API URL. This will connect with SeeTest's Digital Assurance Lab. Here are a few other parameters that will need to be set up within Postman:
- Execution type: Espresso
- Running Type: fast-feedback
- DeviceQuery: [put your device query]
- App: [choose your application file]
- Test App: [choose your test APK file]
- Minimum Devices: put a number [indicating how many devices you'd like your test to run on]
Hit the "Send" button now.
That's all. Now you can see the test running on the QA cloud. You can see the tests executed as well as the ones in progress on the Grid View (in the form of Grid Nodes).
Now, if you go back to the Postman, you can see the URL to the cloud reporting system in the text box (towards the bottom). Clicking on it will take you to the SeeTest Reporter system. There you can view the results of each test in detail. You can view how many tests passed, how many failed, how many are incomplete, and so on. You can see the distribution of version and drill down into any test you like to find the specifics, including the executed steps, step data, device log, as well as a video recording of all the steps involved in a test.
In a nutshell, it's a very powerful feedback mechanism and you can see exactly what has happened. If something fails, it tells you where it failed and what caused it to fail. And if there are any device exceptions in relation to the test, you are able to track that as well make the necessary adjustments without having to spend hours diagnosing the problem.
There is no question that the human input in the software and application development life cycle is irreplaceable. However, this does not override the fact that we, as humans, are prone to making costly mistakes. Automation tools like Espresso are there to supplement our efforts in developing and releasing applications that are reliable, user-friendly, and failure-free. These tools are of the utmost value when it comes to detecting errors in applications prior to their reaching in the hands of the end consumers. If you have not yet incorporated Espresso into your IDE, it's time you gave it a shot! Once you do, it's only natural that you use it in combination with the Experitest Test Automation Platform to get the most out of it.