AmbiguousViewMatcherException multiple RecyclerViews
I have Fragment
one that contains RecyclerView
. But since I have a lot of elements in this Fragment
, I want to cross out the list to see and check all the elements that are in this Fragment
.
This method used to work for me, but now for some reason it doesn't work:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
I have many RecyclerView
with the same id
in my project:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
Also in my tests I wrote something like this:
onView(allOf( withId(R.id.recyclerView), isDisplayed()))
onView(withId(R.id.recyclerView)).perform(swipeUp())
But the error caught is only in the second line.
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.fentury.android:id/recyclerView' matches multiple views in the hierarchy. Problem views are marked with "**** MATCHES ****" below.
source to share
You have multiple views with id R.id.recyclerView
in your view hierarchy, so espresso is not enough to map correctly. Make id
them RecyclerView
unique.
onView(allOf(withId(R.id.recyclerView), isDisplayed()))
onView(withId(R.id.recyclerView)).perform(swipeUp())
But the error caught is only in the second line.
Then do the mapping like this:
onView(allOf(withId(R.id.recyclerView), isDisplayed())).perform(swipeUp())
source to share
You have to use the data to view the recycler where you can assert using the id of the view of the recycler as well as the type of data it stores. This should help to assume that different types of recyclers will not have the same data, but it is better to use different identifiers for different views based on what they are used for.
You can also use the execute function (ViewActions.scrollTo ())
source to share
I had the same problem with multiple RecyclerViews in 2 Fragments inside a ViewPager. Both snippets used the same layout file containing only RecyclerView with id = @ id / list.
Since there was no parent to map to, I made this custom ViewMatcher to map a list by adapter class: (Kotlin)
fun hasAdapterOfType(T: Class<out RecyclerView.Adapter<out RecyclerView.ViewHolder>>): BoundedMatcher<View, RecyclerView> {
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("RecycleView adapter class matches " + T.name)
}
override fun matchesSafely(view: RecyclerView): Boolean {
return (view.adapter.javaClass.name == T.name)
}
}
}
Usage like this:
onView(allOf(withId(list), hasAdapterOfType(AccessAttemptListAdapter::class.java))).check(blabla)
source to share