Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on a specific child of a ListView in Espresso

Using Espresso, I'd like to be able to click on a specific child of an ExpandableListView (named CustomExpandableView). The listview creates a set of RelativeLayouts (named MyContainer).

Ideally, I'd like to click on a specific MyContainer in CustomExpandableView. But, I can live with just clicking the very first one.

The MyContainer objects do not have unique IDs I can reference, but their children do, e.g. - "text=Sample Text Here 1"

I have tried a few different variations of using onData passing the class type and trying to get a child at a specific position, but its just not working. And, I wanted to avoid getting the object and iterating over it until I found the right child.

Here is the portion of the view hierarchy for reference (I removed non-important info from the hierarchy):

+----->CustomExpandableView{} 
|
+------>LinearLayout{}
|
+------->TextView{}
|
+------->FrameLayout{}
|
+-------->BreadCrumbView{}
|
+--------->ImageButton{}
|
+--------->TextView{}
|
+------>LinearLayout{}
|
+------->MyContainer{}
|
+-------->ImageView{res-name=thumb, }
|
+-------->ImageView{res-name=divider}
|
+-------->TextView{res-name=label, text=Sample Text Here 1, input-type=0, ime-target=false}
|
+------->MyContainer{}
|
+-------->ImageView{res-name=thumb}
|
+-------->ImageView{res-name=divider}
|
+-------->TextView{res-name=label text=Sample Text Here 2, input-type=0, ime-target=false}
|
like image 857
JoeyZee Avatar asked Oct 30 '13 22:10

JoeyZee


1 Answers

When you trying to use onData, Espresso tries to find not view by some params, but get Adapter of ListView and search for data in this adapter(by calling Adapter#getItem).

But in the case of ExpandableListView it is not obvious what data to get. And for this purpose Espresso provided onData...().usingAdapterViewProtocol(AdapterViewProtocol). This method javadoc says:

/** * Use a different AdapterViewProtocol if the Adapter implementation does not * satisfy the AdapterView contract like (@code ExpandableListView) */

This AdapterViewProtocol interface looks like : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/google/android/apps/common/testing/ui/espresso/action/AdapterViewProtocol.java .

Examples of AdapterViewProtocol implementation : https://stackoverflow.com/a/40282991/1282732

After correctly defining it, onData will find item view you searched for. After that, if you need to perform an action or check on the child. You just should onData#onChildView.

like image 93
molokoka Avatar answered Oct 10 '22 20:10

molokoka