2. Table Of Content
About Espresso
Espresso Components
Test case structure
Custom Matcher and ViewAction
3. About Espresso
An user interface testing framework for a single
application
Run on devices running Android 2.2 and higher
The Espresso is an instrumentation-based API and
works with the AndroidJUnitRunner test
4. Espresso - Pros
Reliable & synchronizes test actions with UI
operations (on the UI thread).
open to customization with standard hamcrest
matchers
Clear failure, rich debugging information. Allows
custom failure handler
5. Espresso - Cons
Does not support for WebView
Slow speed since it run on Dalvik VM
Work with Single application only.
Solution: Using Espresso combine with uiautomator
6. Espresso Components
ViewMatchers: find view based on: id, Tag,
description, text…
withId(R.id.button)
ViewActions: perform actions on the views
click(), scrollTo()
ViewAssertions: validate state of a view.
matches(isDisplayed()), doesNotExist()
8. Find View
Find the UI component under test in an Activity
by using the onView() or onData() method with a
view matcher which selects the correct view.
9. Perform Actions
Call perform() method and passing in the user
action defined by ViewActions class.
Repeat the steps to simulate a user flow across
multiple activities in the target app
10. Validate the results
Call check() method to check expected state or
behavior
check() method requires a ViewAssertion object
as input
- The framework also ensures that your activity is started before the tests run. It also let the test wait until all observed background activities have finished. Therefore we Dont have to use sleep, retry or loop
- The framework also ensures that your activity is started before the tests run. It also let the test wait until all observed background activities have finished. Therefore we Dont have to use sleep, retry or loop
- If you are using an AdapterView use the onData() method instead of the onView() method
- onView() returns an object of type ViewInteraction.
- onData() method returns an object of type DataInteraction.
Perform method has 2 kinds:
ViewInteraction.perform(): For normal view
DataInteraction.perform(): For view on Adapter
The perform method returns again an object of type ViewInteraction > you can perform more actions or validate the result. It also uses varags as argument, i.e, you can pass several actions at the same time to it.
Matches(Matcher <? Super View> matcher) returns ViewA
Hamcrest Matchers, a framework for writing test matcher objects. Go along with Junit 4.1
Use Hamcrest return TypeSafeMatcher object
Use BoundedMatcher return BoundedMatcher object (same method as Hamcrest)
Hamcrest Matchers, a framework for writing test matcher objects. Go along with Junit 4.1
Use Hamcrest return TypeSafeMatcher object
Use BoundedMatcher return BoundedMatcher object (same method as Hamcrest)
Matches(Matcher <? Super View> matcher) returns ViewA
Matches(Matcher <? Super View> matcher) returns ViewA
Matches(Matcher <? Super View> matcher) returns ViewA
Matches(Matcher <? Super View> matcher) returns ViewA
Matches(Matcher <? Super View> matcher) returns ViewA