单击Recycler视图内部:Espresso

时间:2016-09-26 19:25:49

标签: android automation android-espresso

我正在尝试点击Recycler视图中的菜单项,我有以下代码。

work.links.tasks

和MyViewAction看起来像这样:

onView(allOf(withId(R.id.section_list),isDisplayed())).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("ABC")), MyViewAction.clickChildViewWithId(R.id.payment_menu)));
  • 当我测试它时,我可以看到滚动发生在正确位置,但点击失败并出现以下错误
  

android.support.test.espresso.PerformException:在视图'上执行'android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction@2fd64b56'时出错,ID为:com.em3Agri.operation.debug:id / section_list' 。       在android.support.test.espresso.PerformException $ Builder.build(PerformException.java:83)       在 .........       引起:java.lang.IllegalStateException:位置上没有视图持有者:38       在android.support.test.espresso.contrib.RecyclerViewActions $ ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:290)       在android.support.test.espresso.ViewInteraction $ 1.run(ViewInteraction.java:144)

2 个答案:

答案 0 :(得分:0)

我不知道这是否是您所面临的错误的根本原因,但您应该严格遵守matcheraction之间的区别,即

RecyclerViewActions.actionOnItem(
    hasDescendant(withText("ABC")), 
    MyViewAction.clickChildViewWithId(R.id.payment_menu)
)

应该写成

RecyclerViewActions.actionOnItem(
    <something-that-selects-R.id.payment_menu-in-your-view-item>, 
    click()
)

something-that-selects-R.id.payment_menu-in-your-view-item实际上取决于您的观看设置,可能很难正确定义,因此其他一种方法(例如actionOnHolderItem)可能更适合。这也应该运行得更快,因为它可以直接跳转到由特定视图所标识的视图,而Matcher<View>版本只能滚动&#34;通过您的RecyclerView逐项逐项检查每个新绑定的版本。

答案 1 :(得分:0)

怎么样:

onView(withId(R.id.section_list))
  .perform(
    RecyclerViewActions.actionOnItem(
      hasDescendant(withId(R.id.payment_menu)), 
      ViewActions.click()
    )
  );