Development

How to Do Android Automation Testing with Espresso & UIAutomator

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:

 

How to Do Android Automation Testing with Espresso & UIAutomator

Add to the same build.gradle file the following line in android.defaultConfig:How to Do Android Automation Testing with Espresso & UIAutomator

2.basic sample

How to Do Android Automation Testing with Espresso & UIAutomator

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:How to Do Android Automation Testing with Espresso & UIAutomator

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

 

4.DrawerActions and NavigationViewActions

If you used NavigationView to do the sidebar, you can test it with DrawerActions and NavigationViewActions.How to Do Android Automation Testing with Espresso & UIAutomator

 

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.

How to Do Android Automation Testing with Espresso & UIAutomatorHow to Do Android Automation Testing with Espresso & UIAutomator

 

6.Espresso Intents

When you try to test results from an Intent, you can do it as follows:How to Do Android Automation Testing with Espresso & UIAutomator

 

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.How to Do Android Automation Testing with Espresso & UIAutomator

 

Then I finished testing notifications as follows:

How to Do Android Automation Testing with Espresso & UIAutomator

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:How to Do Android Automation Testing with Espresso & UIAutomatorHow to Do Android Automation Testing with Espresso & UIAutomator

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.

About the Author

More from this Author

Thoughts on “How to Do Android Automation Testing with Espresso & UIAutomator”

  1. Erin Li Post author

    Hi Anton, the “isDialog()” function is used to match roots that are dialogs in Espresso.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories