Android UI Testing
GDG Sacramento
Thomas Amsler
02/12/2015
Espresso (android-test-kit)
Use Espresso to write concise, beautiful, and reliable Android UI tests like this:
android-test-kit : Espresso
public void testSayHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"));
onView(withId(R.id.greet_button)).perform(click());
onView(withText("Hello Steve!")).check(matches(isDisplayed()));
}
Main Components of Espresso
Espresso
Entry point to interactions with views (via onView and onData). Also exposes APIs that are not necessarily tied to any view (e.g. pressBack).
Main Components of Espresso
ViewMatchers
A collection of objects that implement Matcher<? super View> interface. You can pass one or more of these to the onView method to locate a view within the current view hierarchy.
Main Components of Espresso
ViewActions
A collection of ViewActions that can be passed to the ViewInteraction.perform method (for example, click)
Main Components of Espresso
ViewAssertions
A collection of ViewAssertions that can be passed the ViewInteraction.check method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.
Main Components of Espresso
onView(withId(R.id.my_view)) // is a ViewMatcher
.perform(click()) // is a ViewAction
.check(matches(isDisplayed())); // is a ViewAssertion
Targeting View
In a well-behaved application, all views that a user can interact with should either contain descriptive text or have a content description (see Android accessibility guidelines).
Targeting View
If you are not able to narrow down an onView search using 'withText' or 'withContentDescription', consider treating it as an accessibility bug.
Ensure that the application is Accessible
Accessibility
Use the android:contentDescription attribute to label the ImageButton, ImageView, CheckBox and other user interface controls.
Accessibility
Provide an android:hint attribute instead of a content description for EditText fields.
Accessibility
Associate an android:hint attribute with any graphical icons used by controls that provide feedback to the user (for example, status or state information).
Accessibility
Use the uiautomatorviewer tool to ensure that the UI component is accessible to the testing framework.
Tools : UI Automator Viewer
DEMO