1 of 17

Android UI Testing

GDG Sacramento

Thomas Amsler

02/12/2015

2 of 17

Espresso (android-test-kit)

Use Espresso to write concise, beautiful, and reliable Android UI tests like this:

3 of 17

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()));

}

4 of 17

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).

5 of 17

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.

6 of 17

Main Components of Espresso

ViewActions

A collection of ViewActions that can be passed to the ViewInteraction.perform method (for example, click)

7 of 17

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.

8 of 17

Main Components of Espresso

onView(withId(R.id.my_view)) // is a ViewMatcher

.perform(click()) // is a ViewAction

.check(matches(isDisplayed())); // is a ViewAssertion

9 of 17

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).

10 of 17

Targeting View

If you are not able to narrow down an onView search using 'withText' or 'withContentDescription', consider treating it as an accessibility bug.

11 of 17

Ensure that the application is Accessible

12 of 17

Accessibility

Use the android:contentDescription attribute to label the ImageButton, ImageView, CheckBox and other user interface controls.

13 of 17

Accessibility

Provide an android:hint attribute instead of a content description for EditText fields.

14 of 17

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).

15 of 17

Accessibility

Use the uiautomatorviewer tool to ensure that the UI component is accessible to the testing framework.

16 of 17

Tools : UI Automator Viewer

17 of 17

DEMO