android Espresso-toast 消息断言不适用于 sdk 30

问题描述

这个简单的检查适用于 sdk 29 但不适用于 sdk 30:

onView(withText(R.string.text)).inRoot(withDecorView(not(mActivityRule.activity.window.decorView))) .check(matches(isdisplayed()))

我收到 androidx.test.espresso.NoMatchingRootException。

有人可以帮忙解决这个问题吗?

解决方法

Espresso 无法可靠地断言 toast 消息。将 uiAutomator 与 espresso 一起使用来声明祝酒词。

    fun checkToast(msg: String) {
        val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        uiDevice.waitForIdle()
        org.junit.Assert.assertTrue(uiDevice.hasObject(By.text(msg)))
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...