Android recyclerview uitest使用浓缩咖啡,是否有问题?
我的目标是测试RecyclerView滚动。执行示例代码时,会出现以下错误:Android recyclerview uitest使用浓缩咖啡,是否有问题?,android,android-recyclerview,android-espresso,Android,Android Recyclerview,Android Espresso,我的目标是测试RecyclerView滚动。执行示例代码时,会出现以下错误: E/TestRunner: androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to position: 17' on view 'with id: com.jakchang.hwahae:id/recyclerView'. at androidx.test.espresso.PerformExce
E/TestRunner: androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to position: 17' on view 'with id: com.jakchang.hwahae:id/recyclerView'.
at androidx.test.espresso.PerformException$Builder.build(PerformException.java:84)
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=2131296427, 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@209ca0a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=90.0, child-count=0}"
我一直在寻找这个问题,但找不到任何解决办法。这是我的密码。请帮帮我
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Rule
@JvmField
val mainActivity = ActivityTestRule(MainActivity::class.java)
@Test
fun useAppContext() {
//mainActivity.launchActivity(Intent())
Espresso.onView(ViewMatchers.withId(R.id.recyclerView))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(17))
Thread.sleep(10000)
}
}
@RunWith(AndroidJUnit4::class)
类示例InstrumentedTest{
@统治
@JvmField
val mainActivity=ActivityTestRule(mainActivity::class.java)
@试验
fun useAppContext(){
//mainActivity.launchActivity(Intent())
浓缩咖啡onView(ViewMatchers.withId(R.id.recyclerView))
.执行(RecycleServiceActions.scrollToPosition(17))
线程。睡眠(10000)
}
}
根据您的错误日志RecyclerView{…,child count=0}
,您的视图似乎正在异步加载数据,并且调用recycler视图操作太早。最简单的修复方法是在操作之前调用Thread.sleep(…)
或者,您可以在测试用例中使用Espresso的IdlingResource
:
fun waitUntil(matcher: Matcher<View>): ViewAction = object : ViewAction {
override fun getConstraints(): Matcher<View> {
return any(View::class.java)
}
override fun getDescription(): String {
return StringDescription().let {
matcher.describeTo(it)
"wait until: $it"
}
}
override fun perform(uiController: UiController, view: View) {
if (!matcher.matches(view)) {
ViewPropertyChangeCallback(matcher, view).run {
try {
IdlingRegistry.getInstance().register(this)
view.viewTreeObserver.addOnDrawListener(this)
uiController.loopMainThreadUntilIdle()
} finally {
view.viewTreeObserver.removeOnDrawListener(this)
IdlingRegistry.getInstance().unregister(this)
}
}
}
}
}
然后,您可以将操作和匹配器一起使用,等待您的RecyclerView
已填充或准备就绪:
onView(withId(R.id.recyclerView)).perform(
waitUntil(hasItemCount(greaterThan(0))),
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(17)
)
onView(带id(R.id.recyclerView))。执行(
Waitill(hasItemCount(大于(0)),
RecycleServiceActions.scrollToPosition(17)
)
错误日志中的约束条件是什么?更新后的日志在运行测试之前,请确保您的reyclerview中已填充了项。您可以编写@BeforeTest方法,并在我启动活动时填充RecycleServiceWW,即在onCreate时从改装接收的项目
onView(withId(R.id.recyclerView)).perform(
waitUntil(hasItemCount(greaterThan(0))),
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(17)
)