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:
Add to the same build.gradle file the following line in android.defaultConfig:
2.basic sample
3.permissions
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
If you used NavigationView to do the sidebar, you can test it with DrawerActions and NavigationViewActions.
5.RecyclerView
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.
6.Espresso Intents
When you try to test results from an Intent, you can do it as follows:
7.Notification
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
Sometimes we will use alert dialog and toast in our application, and if we want to interact with it, we can check it as following lines:
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.
Hi Erin, thanks for your post.
Could you help, what is the `isDialog()` function?
Hi Anton, the “isDialog()” function is used to match roots that are dialogs in Espresso.
Great Guide.. very helpful.