我在做espresso测试时遇到错误
android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。 在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891) 在android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5751) 在android.view.View.playSoundEffect(View.java:20321) 在android.view.View.performClick(View.java:5636) at nz.salect.handset.auth.EntryActivityUITest.should_open_logDetails_activity_when_pressing_logDetails_button(EntryActivityUITest.kt:47)
第47行是第二次测试中的代码,它尝试执行单击操作。
第一次测试在第二次失败的地方通过,因为它因例外而无法运行。
@LargeTest
@RunWith(AndroidJUnit4::class)
class EntryActivityUITest {
@Rule
@JvmField
val entryAuthActivityRule = ActivityTestRule(EntryActivity::class.java, true, false)
@Test
fun it_should_show_logDetails_buttons_after_starting() {
entryAuthActivityRule.launchActivity(null)
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
@Test
fun should_open_logDetails_activity_after_pressing_logDetails_button() {
entryAuthActivityRule.launchActivity(null)
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
Espresso.onView(ViewMatchers.withId(R.id.loginDetails_textEdit_password))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
这里发生了什么?以及如何解决?
答案 0 :(得分:3)
这两行
val button = entryAuthActivityRule.activity.findViewById<Button>(R.id.auth_button_logDetails)
button.performClick()
看起来很可疑。我本来期待像这样的东西
Espresso.onView(ViewMatchers.withId(R.id.auth_button_logDetails)).perform(ViewActions.click())