lannyf lannyf - 3 months ago 221
Android Question

in Espresso, how to choose one the view who have same id to avoid AmbiguousViewMatcherException

Having gridView which has some images. The gridView's cell comes out from same predefined layout, which has same id and desc.


R.id.item_image == 2131493330


onView(withId(is(R.id.item_image))).perform(click());


Since all cells in the grid have same id, it got
AmbiguousViewMatcherException
.
How to just pick up first one or any of one of them?
Thanks!


android.support.test.espresso.AmbiguousViewMatcherException: 'with id: is <2131493330>' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image, visibility=VISIBLE, width=262, height=262, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES****

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image, visibility=VISIBLE, width=262, height=262, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES****
|

Answer

You should use onData() to operate on GridView:

onData(withId(R.id.item_image))
        .inAdapterView(withId(R.id.grid_adapter_id))
        .atPosition(0)
        .perform(click());

This code will click on the image inside first item in GridView

Comments