Mobile App Automation Testing using ‘ESPRESSO’

Posted by

If you are a Mobile Apps Test Engineer, you cannot overlook the very reliable Google Product i.e. Espresso.

Espresso is an automatic UI testing or as we call it “hands free testing” framework. This is not entirely true as you still have to use your hands to write a code. Once you’ve written the code, Espresso will execute that using advance essentially. It will do all the tasks for you such as clicks on button. It will also swipe and do various things that a normal user does and then verify the test too. So that way, Espresso is pretty exciting.



  • There are many other UI testing libraries out there, but Espresso is special because it is a first party library i.e. google wrote it under the android testing support library.
  • It does automatic synchronization of the test action with the app UI which means, Espresso will watch the UI thread which is the thread that renders all the pixels on the screen. So while the pixels render on screen Espresso will sleep for you, until the UI thread is idle.
  • Easy to set up, fast execution.
  • The API is relatively small, yet open for customization.
  • Hands free testing
  • Junit4


Here is how we can get going with Espresso.

 Adding Espresso to your project

  • First of all make sure you have Android Support Repository installed


  • Add the following dependencies to your application build. gradle file.


  • Finally, specify the test instrumentation runner in default config in build. gradle file.



Configure Device Settings


Turn off animations on your test device — To avoid unexpected results, turn off animations from Settings by opening Developer options and turning all the following options off:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale


Main Components of Espresso

Espresso has Three Main Components:

  • ViewMatchers– allows you to locate a view in the current view hierarchy (eg. Find Something)
  • ViewActions– allows you to interact with views(eg. do something)
  • ViewAssertions– allows you to assert the state of a view.(eg. check something)

Now, let’s find out how these components find their place. This is what I call ‘the  formula’.


My first Espresso test

In this example, I take one edit text and one button. Now, we have to ensure that on onButtonClick(), the input string matches with the given string.



Here is my code –


By using ActivityTestRule, espresso framework launches the activity under test before each test method annotated with @Test and before any method annotated with @Before. In my case, HelloWorldDemo is my LauncherActivity.


The method under annotation @Before basically execute before the @Test Method executes.


This is the Test case with Annotation @Test


Well, all set now. Right click on your Test and Select Run. Following Screen will come as a result Screen.



Happy Coding!!!



Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *