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:
Launch Animal Gallery app
“Donation” screen will be shown up. Press “Later” button to proceed to main screen
Test on functionality of the grid view
Scrolls to the 3rd image and tap on it.
In full screen viewpager, swipe to the left and view the next image.
Back to the main screen
Scrolls to the 6th image and tap on it.
In full screen viewpager, swipe to the left twice and view the image.
Swipe to the right again to go back the previous image.
Test on functionality of the navigation drawer
Tab on the navigation drawer icon to open the drawer
Search for “Flickr: Dogs” category and tab on it.
Open the drawer again and select “Tutorial” category.
publicvoidtestGridView(){//Close the dialog and press later buttononView(withId(R.id.later_button)).perform(click());onView(isRoot()).perform(waitAtLeast(2000));//Scroll the 3rd image of view and select itonData(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 screenEspresso.pressBack();//Scroll the 5th image of view and select itonData(anything()).inAdapterView(allOf(withId(R.id.gridview),isDisplayed())).atPosition(5).check(matches(isDisplayed())).perform(click());onView(isRoot()).perform(waitAtLeast(2000));//Do the swipingonView(withId(R.id.pager)).perform(swipeLeft());onView(withId(R.id.pager)).perform(swipeLeft());onView(withId(R.id.pager)).perform(swipeRight());//Back to main screenEspresso.pressBack();}publicvoidtestNavDrawer(){//Click on the later buttononView(withId(R.id.later_button)).perform(click());//Open and close the draweronView(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 categoryonView(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 tutorialonView(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 screenonView(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 activityEspresso.pressBack();onView(isRoot()).perform(waitAtLeast(2000));}