【发布时间】: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