Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - click an image inside a gridview

I have a view hierarchy like below:

GridView{id=2131362110, res-name=item_list_grid, 
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+------>RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
GridView{id=2131362110, res-name=item_list_grid, ...etc

I want to perfom click on one of the ImageView with id=item_image.

I can not use something like atPosition(x) together with onView so instead I used onData. I tried all of these:

onData(allOf(anything(),withId(R.id.item_image))).atPosition(0).perform(click());

onData(anything()).atPosition(0).perform(click());

onData(allOf(atPosition(0),withId(R.id.item_image))).perform(click());

But all resulted in

android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.

Any suggestions for this? Thanks!

like image 343
Great Question Avatar asked Nov 30 '22 00:11

Great Question


1 Answers

Your error message tell you have multiple views in your activity that extends form AdapterView so you have another ListView or GridView in your layout.

You can either select the AdapterView on the data layer. So select this AdapterView with items of type ItemModel

onData(is(instanceOf(ItemModel.class))).atPosition(0)
    .onChildView(withId(R.id.item_image)).perform(click());

or you can choose a specific AdapterView by id

onData(anything()).inAdapterView(withId(R.id.my_grid_view)).atPosition(0).
            onChildView(withId(R.id.item_image)).perform(click());
like image 85
David Boho Avatar answered Dec 12 '22 01:12

David Boho