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 -
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 |
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 |
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 |
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 |
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 :-)
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
What is ListViewSample.ROW_TEXT?
ReplyDeleteEven i got the same doubt.SO if you knew it already,please let me know.
DeleteHi,
ReplyDeletehow i can clear listview?
What is ListViewSample.ROW_TEXT?
ReplyDeleteI tried your code. But there is error encountered. Can you help how to solve this issue.
ReplyDeletejava.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]]
Can you please show an example of gridview.
ReplyDelete