【问题标题】:Espresso test on RecyclerViewRecyclerView 上的 Espresso 测试
【发布时间】:2020-04-03 15:00:16
【问题描述】:

我开始测试驱动开发,但一开始我就遇到了一个错误。 我不明白这个例外:

E/TestRunner: androidx.test.espresso.PerformException: Error performing 'actionOnItemAtPosition performing ViewAction: single click on item at position: 0' on view 'with id: com.jakchang.idus:id/recyclerView'. at blabla~
     Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
    (is assignable from class: class androidx.recyclerview.widget.RecyclerView and is displayed on the screen to the user)
    Target view: "RecyclerView{id=2131230893, res-name=recyclerView, visibility=VISIBLE, width=1080, height=0, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@1a52327, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}"
        at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:252)
        at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:65)
        at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:158)
        at androidx.test.espresso.ViewInteraction$1.call

下面是我的代码

@JvmField
@Rule
var mActivityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

@Test
fun recyclerTest(){
    onView(withId(R.id.homeIcon)).perform(click())
    onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,click()))
}

活动何时开始且仅使用

onView(withId(R.id.homeIcon)).perform(click())

似乎可以工作,但是当以下行被执行时

onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,click()))

出现错误。

层结构是这样的

constraint-layout
  -linear-layout
  --home_icon
 -scroll view
  --linear-layout
   ---recyclerview
   ---progressbar

我该如何解决这个问题?

【问题讨论】:

  • 此时屏幕上是否向用户显示RecyclerView
  • 您的RecyclerView 在您执行操作时没有子视图。也许您的列表是异步加载的?
  • 是 RecyclerView 是异步加载的所以显示出来了

标签: android android-recyclerview junit4 android-espresso


【解决方案1】:

问题可能是在执行actionOnItemAtPosition 指令之前,您可能必须先滚动ScrollView,直到RecyclerView 显示给用户。

尝试在此之前添加onView(withId(R.id.recyclerView)).perform(ViewActions.scrollTo());。测试将是:

@Test
fun recyclerTest(){
    onView(withId(R.id.homeIcon)).perform(click());
    onView(withId(R.id.recyclerView)).perform(ViewActions.scrollTo(),
       RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,click()));
}

【讨论】:

  • 先前的错误已被删除,但新的错误出现在我身上。那是 androidx.test.espresso.PerformException:在视图 'with id: com.jakchang.idus:id/recyclerView' 上执行“滚动到”时出错 bla bla~ 原因:java.lang.RuntimeException:尝试滚动到视图,但视图没有显示....
  • 你的ScrollView 有填充吗?如果是这样,你能把它移到里面的LinearLayout再试一次吗?
  • 我在滚动视图中没有填充和边距,并在 LinearLayout 中添加了滚动视图。但结果是一样的..
  • 我在 recyclerview 上用一个线性布局测试了这个,没有其他的。它正在工作。但是当我在一些复杂的布局上使用它时,它就不起作用了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
相关资源
最近更新 更多