AmbiguousViewMatcherException多个RecyclerViews

Mor*_*zov 6 android swipe android-fragments android-espresso android-recyclerview

我有一个Fragment包含RecyclerView。但是,由于其中包含很多元素,因此Fragment我想向上滑动列表以查看并检查其中的所有元素Fragment

早期,此方法对我有帮助,但现在由于某种原因它不起作用:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
Run Code Online (Sandbox Code Playgroud)

我的项目中有很多RecyclerView相同的id

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"/>
Run Code Online (Sandbox Code Playgroud)

同样在测试中,我写了这样的东西:

onView(allOf( withId(R.id.recyclerView), isDisplayed()))
onView(withId(R.id.recyclerView)).perform(swipeUp())
Run Code Online (Sandbox Code Playgroud)

但是仅在第二行捕获了错误。

android.support.test.espresso.AmbiguousViewMatcherException:'with id:com.fentury.android:id/recyclerView'匹配层次结构中的多个视图。问题视图在下面标有“ **** MATCHES ****”。

azi*_*ian 6

R.id.recyclerView的视图层次结构中有多个带有 id的视图,因此 espresso 无法执行正确的匹配。使id那些RecyclerViews 的 s 唯一。


onView(allOf(withId(R.id.recyclerView), isDisplayed())) onView(withId(R.id.recyclerView)).perform(swipeUp())

但仅在第二行捕获错误。

然后以这种方式执行匹配:

onView(allOf(withId(R.id.recyclerView), isDisplayed())).perform(swipeUp())
Run Code Online (Sandbox Code Playgroud)