Aaron Liew's Blog

Blog about Android Development

White-Box Testing- Espresso

One common approach to UI testing is to run tests manually and verify that the app is behaving as expected. However, this approach can be very time-consuming, tedious, and error-prone especially when we run tests on multiple devices. This is where Android Automated Tests becomes useful, it can perform repetitive task without human intervention.

What is White-box Testing?

There are two types tests: white-box test and black-box test. White-box test is method of testing software that tests internal structures or workings of an application. Black-box test is method of examining the functionality of an application without peering into its internal structures or workings. In this project, I did white-box test on my code using Espresso library.

Advantage of Espresso

The advantage of the Espresso is it is have synchronization feature, meaning it waits for UI events in the current message queue to process and default task to complete before it moves on to the next test operation.

Demonstration of the Espresso

I have written test script on animal gallery app. Here are the following steps to verify the functionality of the app:

  1. Launch Animal Gallery app
  2. “Donation” screen will be shown up. Press “Later” button to proceed to main screen

Test on functionality of the grid view

  1. Scrolls to the 3rd image and tap on it.
  2. In full screen viewpager, swipe to the left and view the next image.
  3. Back to the main screen
  4. Scrolls to the 6th image and tap on it.
  5. In full screen viewpager, swipe to the left twice and view the image.
  6. Swipe to the right again to go back the previous image.

Test on functionality of the navigation drawer

  1. Tab on the navigation drawer icon to open the drawer
  2. Search for “Flickr: Dogs” category and tab on it.
  3. Open the drawer again and select “Tutorial” category.
  4. Swipes the tutorial screen.
  5. Done.

Here is the demonstration of the test:

Coding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 public void testGridView() {
        //Close the dialog and press later button
        onView(withId(R.id.later_button))
        .perform(click());
        onView(isRoot()).perform(waitAtLeast(2000));

        //Scroll the 3rd image of view and select it
        onData(anything())
        .inAdapterView(allOf(withId(R.id.gridview), isDisplayed()))
        .atPosition(2)
        .check(matches(isDisplayed()))
        .perform(click());
        onView(isRoot()).perform(waitAtLeast(2000));

        //Do the swiping 
        onView(withId(R.id.pager))
          .perform(swipeLeft());

        //Back to the main screen
        Espresso.pressBack();

        //Scroll the 5th image of view and select it
        onData(anything())
                .inAdapterView(allOf(withId(R.id.gridview), isDisplayed()))
                .atPosition(5)
                .check(matches(isDisplayed()))
                .perform(click());
        onView(isRoot()).perform(waitAtLeast(2000));

        //Do the swiping
        onView(withId(R.id.pager))
                .perform(swipeLeft());
        onView(withId(R.id.pager))
                .perform(swipeLeft());
        onView(withId(R.id.pager))
                .perform(swipeRight());

        //Back to main screen
        Espresso.pressBack();


}


public void testNavDrawer(){

        //Click on the later button
        onView(withId(R.id.later_button))
                .perform(click());

        //Open and close the drawer
        onView(withContentDescription(getActivity().getString(R.string.drawer_open)))
          .perform(click());
        onView(withContentDescription(getActivity().getString(R.string.drawer_close)))
          .perform(click());

        //Open the drawer and select dog category
        onView(withContentDescription(getActivity().getString(R.string.drawer_open)))
          .perform(click());
        onView(Matchers.allOf(ViewMatchers.withId(R.id.sm_item_title), hasSibling(ViewMatchers.withText("Flickr: Dogs")))).perform(click());

        //Open the drawer again and select tutorial
        onView(withContentDescription(getActivity().getString(R.string.drawer_open)))
           .perform(click());
        onView(isRoot()).perform(waitAtLeast(2000));
        onView(withId( R.id.tutorial)).perform( scrollTo(), click());

        //Do the swiping on tutorial screen
        onView(withId(R.id.tutorial_pager))
        .perform(swipeLeft());
        onView(withId(R.id.tutorial_pager))
                .perform(swipeLeft());
        onView(withId(R.id.tutorial_pager))
                .perform(swipeRight());

        //Back to previous activity
        Espresso.pressBack();
        onView(isRoot()).perform(waitAtLeast(2000));

}