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

我很努力地想出如何做到这一点,因为与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”的作用类似于


在列表中找到项目数
依次滚动到每个滚动对象(scrollToPosition(i))
检查该行是否有我想要的项目。就像是

StringUtils.equals(viewHolder.nameView.getText().toString(), assetName);

最佳答案

我在此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与您要查找的项目进行匹配。

10-07 19:19
查看更多