Espresso 在 RecyclerView 中发现可能不在屏幕上的行

Espresso finding row in RecyclerView that could be off screen

我有一个 RecyclerView,它可以包含任意数量的项目。我需要能够从该列表中获取一行,以便我可以检查其内容或 click/long 单击该行。

我正在努力弄清楚如何做到这一点,因为与 ListView "onData" 不同,没有内置功能可以滚动 RecyclerView 来查找行。如果使用 RecyclerView,如果视图实际上不在屏幕上,您似乎无法验证行 UI.

在我看到的示例中,他们使用列表中的项目数滚动到每一行。但是我事先不知道我的列表有多少行,因为数字不固定。

这个解决方案看起来很有前途,但前提是您知道列表中要传递给 espresso 对象的项目,这对我不适用。

https://medium.com/@_rpiel/recyclerview-and-espresso-a-complicated-story-3f6f4179652e

这个例子还假设它知道哪些行不在屏幕上 https://github.com/googlesamples/android-testing/blob/master/ui/espresso/RecyclerViewSample/app/src/androidTest/java/com/example/android/testing/espresso/RecyclerViewSample/RecyclerViewSampleTest.java

同样这个例子知道要移动到哪一行 http://blog.forkingcode.com/2016/06/espresso-and-recyclerview.html

我需要做类似

的事情
onView(withId(R.id.recyclerView)).perform(findRowWhichHasText("name1");

其中 "findRowWhichHasText" 做类似

的事情

我在这个 wiki 页面中找到了解决方案。 http://alexander-thiele.blogspot.com/2016/01/espresso-ui-tests-and-recyclerview.html

他的代码onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withHolderTimeView(getActivity().getString(R.string.all_day_event))));

其中 withHolderTimeView 是一个匹配器,它将 Viewholder 与您要查找的项目相匹配。