Espresso 提供了滚动到特定项或对特定项执行操作的机制,适用于两种类型的列表:适配器视图和回收器视图。
处理列表(尤其是使用 RecyclerView 或 AdapterView 对象创建的列表)时,您感兴趣的视图甚至可能不会显示在屏幕上,因为系统只会显示少量项,并在您滚动时回收这些项。在这种情况下,无法使用 scrollTo() 方法,因为它需要现有视图。
与适配器视图列表项交互
请勿使用 onView() 方法,而是使用 onData() 开始搜索,并提供一个匹配器来匹配支持您要匹配的视图的数据。Espresso 将完成在 Adapter 对象中查找行并使该项在视口中可见的所有工作。
使用自定义视图匹配器来匹配数据
以下 activity 包含一个 ListView,它由一个 SimpleAdapter 提供支持,该
包含 Map<String, Object> 对象中每一行的数据。

每个映射都有两个条目:一个包含字符串(例如 "item: x")的键 "STR",以及一个包含 Integer(表示内容长度)的键 "LEN"。例如:
{"STR" : "item: 0", "LEN": 7}
要点击包含“item: 50”的行,代码如下所示:
onData( allOf( `is`(instanceOf(Map::class.java)), hasEntry(equalTo("STR"), `is`("item: 50")) ) ).perform(click())
请注意,Espresso 会根据需要自动滚动浏览列表。
接下来,我们拆开 onData() 内的 Matcher<Object>。is(instanceOf(Map.class)) 方法会将搜索范围缩小到 AdapterView 的任何项,该项由 Map 对象支持。
在本例中,查询的这一方面与列表视图的每一行都匹配,但我们希望专门点击某个项,因此我们使用以下代码进一步缩小搜索范围:
hasEntry(equalTo("STR"), `is`("item: 50"))
此 Matcher<String, Object> 将匹配任何包含键为 "STR" 且值为 "item: 50" 的条目的 Map。由于查找此内容的代码很长,并且我们希望在其他位置重复使用,因此我们来为此编写一个自定义 withItemContent 匹配器:
return object : BoundedMatcher<Any, Map<*, *>>(Map::class.java) { override fun matchesSafely(map: Map<*, *>): Boolean { return hasEntry(equalTo("STR"), itemTextMatcher).matches(map) } override fun describeTo(description: Description) { description.appendText("with item content: ") itemTextMatcher.describeTo(description) } }
您可以使用 BoundedMatcher 作为基础,因为仅匹配 Map 类型的对象。替换 matchesSafely() 方法,放入之前找到的匹配器,并将其与您可以作为参数传递的 Matcher<String> 进行匹配。这样您就可以调用 withItemContent(equalTo("foo")) 了。为使代码简洁,您可以创建另一个匹配器,该匹配器已调用 equalTo() 并接受 String 对象:
fun withItemContent(expectedText: String): Matcher<Any> { checkNotNull(expectedText) return withItemContent(equalTo(expectedText)) }
现在,要点击该项目,代码非常简单:
onData(withItemContent("item: 50")).perform(click())
如需查看此测试的完整代码,请查看 GitHub 上的 AdapterViewTest 类中的 testClickOnItem50() 方法以及此自定义 LongListMatchers 匹配器。
匹配特定子视图
上面的示例在 ListView 的整行中间发出点击。但是,如果我们要对行的特定子级执行操作,该怎么办呢?例如,我们想要点击 LongListActivity 行的第二列,该列显示第一列中内容的 String.length:

只需将 onChildView() 规范添加到 DataInteraction 的实现中即可:
onData(withItemContent("item: 60")) .onChildView(withId(R.id.item_size)) .perform(click())
与回收器视图列表项交互
RecyclerView 对象的运作方式与 AdapterView 对象不同,因此无法使用 onData() 与其交互。
如需使用 Espresso 与 RecyclerView 交互,您可以使用 espresso-contrib 软件包,其中包含一系列 RecyclerViewActions,可用于滚动到特定位置或对项执行操作:
scrollTo()- 滚动到匹配的 View(如果存在)。scrollToHolder()- 滚动到匹配的 View Holder(如果存在)。scrollToPosition()- 滚动到特定位置。actionOnHolderItem()- 对匹配的视图持有者执行视图操作。actionOnItem()- 对匹配的视图执行视图操作。actionOnItemAtPosition()- 在特定位置对视图执行视图操作。
以下代码段展示了 RecyclerViewSample 示例中的一些示例:
@Test(expected = PerformException::class) fun itemWithText_doesNotExist() { // Attempt to scroll to an item that contains the special text. onView(ViewMatchers.withId(R.id.recyclerView)) .perform( // scrollTo will fail the test if no item matches. RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>( hasDescendant(withText("not in the list")) ) ) }
@Test fun scrollToItemBelowFold_checkItsText() { // First, scroll to the position that needs to be matched and click on it. onView(ViewMatchers.withId(R.id.recyclerView)) .perform( RecyclerViewActions.actionOnItemAtPosition( ITEM_BELOW_THE_FOLD, click() ) ) // Match the text in an item below the fold and check that it's displayed. val itemElementText = "${activityRule.activity.resources .getString(R.string.item_element_text)} ${ITEM_BELOW_THE_FOLD.toString()}" onView(withText(itemElementText)).check(matches(isDisplayed())) }
@Test fun itemInMiddleOfList_hasSpecialText() { // First, scroll to the view holder using the isInTheMiddle() matcher. onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.scrollToHolder(isInTheMiddle())) // Check that the item has the special text. val middleElementText = activityRule.activity.resources .getString(R.string.middle) onView(withText(middleElementText)).check(matches(isDisplayed())) }
其他资源
如需详细了解如何在 Android 测试中使用 Espresso 列表,请参阅以下资源。
示例
- DataAdapterSample:展示 Espresso 的
onData()入口点,适用于列表和AdapterView对象。