我有一个包含 RecyclerView 的 Fragment。但是因为我在这个 Fragment 中有很多元素,所以我想向上滑动列表以查看并检查这个 Fragment 中的所有元素。
早些时候这个方法对我有帮助,但现在由于某种原因它不起作用:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
我的项目中有许多具有相同 id 的 RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
同样在我的测试中,我写了这样的东西:
onView(allOf( withId(R.id.recyclerView), isDisplayed()))
onView(withId(R.id.recyclerView)).perform(swipeUp())
但只在第二行捕获到错误。
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.
最佳答案
您的 View 层次结构中有多个 ID 为 R.id.recyclerView 的 View ,因此 espresso 无法执行正确的匹配。使那些 RecyclerView 的 id 是唯一的。
onView(allOf(withId(R.id.recyclerView), isDisplayed()))onView(withId(R.id.recyclerView)).perform(swipeUp())But caught error only on second line.
然后这样进行匹配:
onView(allOf(withId(R.id.recyclerView), isDisplayed())).perform(swipeUp())
关于android - AmbiguousViewMatcherException 多个 RecyclerViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44538552/