What is Espresso in Android Testing ?




Espresso is an Android UI testing library. Using Espresso you can interact with views and check their state.

There are 4 basic parts that make up an espresso statement. 

  1. Static Espresso method 
  2. ViewMatcher 
  3. ViewAction 
  4. ViewAssertion 


Example of Espresso Statement: 

onView(withId(R.id.taskdetail_checkbox))
            .perform(click())
            .check(matches(isChecked()))


onView  -> Static Espresso Function (it basically says we are going to something with the view )
withId-> ViewMatcher (it gets the id of the view)

Note* we get AmbiguousViewMatcherException when your ViewMatcher identifies more than one View. 

click()-> ViewAction (perform() method takes a ViewAction, which does something to a view )
matches(isChecked())-> ViewAssertion (a call to check, which actually asserts something about the view )



Dependencies to add to your build.gradle(Module) file to use Espresso :

// Espresso TestingandroidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'androidTestImplementation 'androidx.test.espresso:espresso-contrib: 3.2.0'


Espresso-Core library contains the matchers and view assertions.
It is also included by default when you make a new Android project.

Espresso-Contrib library is made up of external contributions hence contains testing code for more advanced views such as the date picker and recycler view. It contains the Accessibility checks and also contains a class called counting idling resources.


Espresso Test always runs in real and emulated device as it is instrumented test by nature. One thing that can make your Espresso Test flaky is if you leave on your device's default animations. If there's some sort of animation in your app that causes a lag an you try to test whether a view is onscreen or whether it's still animating Espresso can sometimes accidentally fail a test.
So for special UI testing the best practice is to turn off animations and your tests will run faster.

  1. On your testing device, go to Settings > Developer options.
  2. Disable these three settings: Window animation scale, Transition animation scale and Animator duration scale:




N.K.





Comments

Popular Posts