Espresso - check if TextView exists in ListView

I want to check the display of Save € XX in the list. Save € XX is a TextView that can be VISIBLE or INVISIBLE . I am using JUnit 4 and Espresso 2.2.1.

I tried to check it like this:

 onView(withText(startsWith("Save"))).check(matches(isDisplayed())); 

but always get the error:

 android.support.test.espresso.AmbiguousViewMatcherException: 'with text: a string starting with "Save"' matches multiple views in the hierarchy. 

Is there a way if a TextView exists in a ListView with espresso?

UPDATE

I also tried using onData :

 onData(hasToString(startsWith("Save"))) .inAdapterView(withId(R.id.suggestion_list_view)).atPosition(0) .check(matches(isDisplayed())); 

but it seems that onData works with the data layer, but not with the presentation layer. So I get the error message:

 java.lang.RuntimeException: No data found matching: with toString() a string starting with "Save" contained values: <[Data: ...]> 

enter image description here

+7
android junit junit4 android-espresso
source share
1 answer

After several attempts, I found a way.

In this case, we must use a combined approach and work with both layers of data and presentation. We get the ListView by ID and select the first item. Then check it on "Save . "

 onData(anything()) .inAdapterView(withId(R.id.list_view)) .atPosition(0) .onChildView(withId(R.id.suggestion_saving)) .check(matches(withText(startsWith("Save")))); 

It works like a charm. Enjoy it!

+14
source share

All Articles