Friday, 29 January 2016

Testing List View with Espresso Data Adapter



Espresso Data Adapter is used for testing of  Adapter Views like ListView & GirdView. In short Adapter Views is view whose children are bounded by an Adapter. In this case we need to use onData() instead if onView() because onData() is the one who matches the Data bound to the view item.


Video Tutorial -


I am going to explain testing of ListView using Espresso in detail. Below is the screenshot of the App with 50 List items. Each row contains a TextView and a ToggleButton. At the top of there is Text "Clicked on row no." which will show the row number on which you just clicked. The ToggleButton if checked displays text "checked". This is all about the ListView which we are going to test now.

Espresso Data Adapter
Espresso Data Adapter
1.Testing Scrolling functionality of ListView - In this we need to test scrolling to particular position and scrolling to the end of ListView


@Testpublic void testScrollingAt()throws Exception
{
    // Scrolls till the position 20 

 onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 20")))

    .check(matches(isCompletelyDisplayed()));
    //scrolls till the end     

onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 49")))

    .check(matches(isCompletelyDisplayed()));
}

 
Testing List Items with Espresso Data Adapter
Testing List Items with Espresso
2. Testing clicking on particular List item and checking the List item is selected.


@Testpublic void testClickOnListItem()
{
    onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 25")))
            .onChildView(withId(R.id.rowTextView)).perform(click());
    onView(withId(R.id.selection_row_value)).check(matches(withText("25")));
}

 
Testing List with Espresso Adapter
Testing List with Espresso Adapter
4. Testing Toggle button for particular row



@Testpublic void testToggleButton()throws Exception
{
    onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 25")))
            .onChildView(withId(R.id.rowToggleButton)).perform(click());
    Thread.sleep(1000);
    onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 25")))
            .onChildView(withId(R.id.rowToggleButton)).check(matches(isChecked()));
}
Testing List with Espresso Adapter
Testing List with Espresso Adapter

I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)

Download sample code from - https://github.com/googlesamples/android-testing

6 comments:

  1. Hi,

    how i can clear listview?

    ReplyDelete
  2. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Mobile App Development Company
    Mobile App Development Company
    Mobile app Development Companies

    ReplyDelete
  3. I tried your code. But there is error encountered. Can you help how to solve this issue.

    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/hamcrest/integration/EasyMock2Adapter;

    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.integration.EasyMock2Adapter" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.savvysherpa.t2deventful.test-2/base.apk", zip file "/data/app/com.savvysherpa.t2deventful-1/base.apk"],nativeLibraryDirectories=[/data/app/com.savvysherpa.t2deventful.test-2/lib/arm, /data/app/com.savvysherpa.t2deventful-1/lib/arm, /vendor/lib, /system/lib]]

    ReplyDelete
  4. Can you please show an example of gridview.

    ReplyDelete