gpt4 book ai didi

android - android espresso 测试时如何解决这个 CalledFromWrongThreadException?

转载 作者:行者123 更新时间:2023-11-29 16:42:55 37 4
gpt4 key购买 nike

我在做 Espresso 测试时出错

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891) at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5751) at android.view.View.playSoundEffect(View.java:20321) at 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()))
}
}

这里发生了什么?以及如何解决?

最佳答案

这两行

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

关于android - android espresso 测试时如何解决这个 CalledFromWrongThreadException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894972/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com