【问题标题】:How to solve this CalledFromWrongThreadException when doing android espresso test?在进行 android espresso 测试时如何解决这个 CalledFromWrongThreadException?
【发布时间】:2018-04-18 16:42:02
【问题描述】:

我在进行 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) 在 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()))
    }
}

这里发生了什么?又该如何解决呢?

【问题讨论】:

    标签: android kotlin android-espresso android-instrumentation


    【解决方案1】:

    这两行

        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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多