使用android espresso测试带有多个片段的ViewPager

Uba*_*hat 9 android automated-tests android-espresso

我正在尝试测试我使用的应用程序ViewPager.每个页面都包含片段,但这些片段并不总是可见.我想检查当前可见页面中片段的可见性.

onView(withId(R.id.container_weather))
    .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
Run Code Online (Sandbox Code Playgroud)

但问题是espresso外观不仅是当前页面的所有页面而且我收到以下错误:

android.support.test.espresso.AmbiguousViewMatcherException:'with id:eu.airpatrol.android:id/container_weather'匹配层次结构中的多个视图...

skb*_*hmn 9

我遇到了同样的问题,但是使用条件isCompletelyDisplayed()解决了这个问题,因为它只考虑了屏幕视图。

所以,这样的事情应该工作:

onView(allOf(withId(R.id.container_weather), isCompletelyDisplayed()))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
Run Code Online (Sandbox Code Playgroud)

注意:isDisplayed()在某些情况下也可以工作,但它也会考虑屏幕外的视图,如果 ViewPager 有任何其他页面 pr 片段加载了相同的视图 ID,则它将不起作用。


den*_*nys 5

由于具有相同ID的多个元素,您的测试失败.您可以使用组合条件allOf(...).然后用于isDisplayed()检查屏幕上是否显示匹配的视图.下面的例子可以工作:

onView(allOf(
    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
    withId(R.id.container_weather)))
    .check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

  • 两者之间的唯一区别是y坐标,因为两者在不同的页面上.:( (3认同)