About Espresso and UIAutomator
Espresso is a powerful, efficient Android Automation Test Framework, it contains three parts: ViewMatchers, ViewActions and ViewAssertions. Compared with other frameworks, it is simpler and easier for us to learn, but there is a limitation: you can only do operations in your tested app. So, if you want to deal with notifications or interact with other applications, you will need to combine it with UIAutomator. UIAutomator is a UI testing framework suitable for cross-app functional UI testing and since UIAutomator is now also based on Android Instrumentation, we can easily use it with Espresso in one test suite.
Begin with code
1.configure your project
Open your app’s build.gradle file and add the following lines inside dependencies:
When we launch an App for the first time, if your Android version is above 5, it will ask for permissions. But it is not a context inside our tested App, so we need to use UIAutomator as in the following lines:
4.DrawerActions and NavigationViewActions
We always use RecyclerView in our App in order to interact with RecyclerView using Espresso. The espresso-contrib package has a collection of RecyclerViewActions that can be used to scroll to positions or to perform actions on items.
I was blocked when I tried to get element in a notification, then I found a useful tool. UIAutomatorViewer is helpful for us to analyze UI components, and you can find it in your Android->sdk->tools folder.
Then I finished testing notifications as follows:
8.Alert dialog and Toast
This blog is just a glimpse of an Android Automation Test, there are still many things left to be researched. I hope we can make greater progress together.