Fabio De Lorenzo
Fabio De Lorenzo

Reputation: 111

Android Espresso click a button inside a ListView

I'm trying to make a test with Espresso that performs a click on a button within a specific element of my Listview.

Each element in the ListView has a a button (R.id.my_button) and I want to click the button of the first element.

I tried: onData(anything()). atPosition(0). inAdapterView( allOf( withId(R.id.my_button) ) ). perform(click());

Actual result: androidx.test.espresso.AmbiguousViewMatcherException: '(with id: com.test.test:id/my_button)' matches multiple views in the hierarchy.

Upvotes: 1

Views: 972

Answers (2)

Zerosero
Zerosero

Reputation: 343

This might help, Go inside listView -> first element -> first element's child (in your case the button)

onData(anything()).inAdapterView(withId(R.id.list)).atPosition(0).onChildView(withId(R.id.my_button)).check(matches(isDisplayed())).perform(click());

Upvotes: 2

davood esmaeily
davood esmaeily

Reputation: 1

If you want to go inside AdapterView, this will allow you to click an element inside listview

onData(anything()).inAdapterView(withId(R.id.list1)).atPosition(0).perform(click());

Upvotes: 0

Related Questions