首页 > 解决方案 > 如何单击 androidx.test.espresso 上的指定 recyclerview?

问题描述

我有问题点击此处指定回收站查看我的代码,它仍然错误

Espresso.onView(withId(R.id.recyclerLastMatchlist))
    .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(1, ViewActions.click()))

这是依赖

androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'


androidTestImplementation("com.android.support.test.espresso:espresso-contrib:2.2.2") {
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}

标签: androidkotlinandroid-espressoandroid-instrumentation

解决方案


根据您在 gradle 中的依赖项,您的应用似乎正在使用com.android.support,而不是androidx. 如果是这种情况,您会得到,NoViewMatchException因为您的测试正在尝试对androidx.recyclerview.widget.RecyclerView而不是执行操作android.support.v7.widget.RecyclerView,这显然是不同的。

所以尝试替换你的依赖项:

//androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
//androidTestImplementation 'androidx.test:runner:1.1.0'
//androidTestImplementation 'androidx.test:rules:1.1.0'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

推荐阅读