测试RecyclerView:RecyclerViewActions不够滚动

时间:2018-03-02 15:50:00

标签: android testing android-recyclerview android-espresso

我希望我的espresso测试能够向下滚动RecyclerView,直到它看到某个视图并点击它。

因此我使用RecyclerViewActions

onView(withId(R.id.recycler)).perform(
          RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
                  hasDescendant(withText(R.string.title_item_x))
          )
)
Thread.sleep(2000)
onView(withId(R.id.recycler)).perform(
          RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>(
                  hasDescendant(withText(R.string.title_item_x)),
                  click()
          )
)

测试向下滚动RecyclerView,但只占所需数量的50%左右。点击通话因此而崩溃。

有人遇到过类似的东西吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了为什么视图操作没有成功的原因:

该应用有一个可折叠工具栏,在我开始滚动操作之前已展开。当我执行滚动操作时,工具栏首先折叠,然后滚动回收器视图。因此,回收者视图滚动到很少。

我找到的解决方案是首先使用自定义AppBarViewAction折叠工具栏。找到解决方案here