android - 如何检查不显示弹出通知。 Espresso

标签 android kotlin android-espresso

我有弹出通知。我的第一个测试检查该通知是否显示并成功通过。我对此测试使用下一个方法:

fun viewInNotificationPopupIsDisplayed(viewMatcher: Matcher<View>) {
    onView(viewMatcher)
        .inRoot(RootMatchers.isPlatformPopup())
        .check(ViewAssertions.matches(isDisplayed()))
}

我对第二个测试用例有问题,我必须检查我的弹出通知是否已经消失(意味着它不再显示)。
所以我正在尝试使用下一个方法:
    fun viewInNotificationPopupIsNotDisplayed(viewMatcher: Matcher<View>) {
        Espresso.onView(viewMatcher)
            .inRoot(RootMatchers.isPlatformPopup())
            .check(matches(not(isDisplayed())))
          //.check(ViewAssertions.doesNotExist())  // doesn't work as well
    }

我得到下一个异常(exception):
android.support.test.espresso.NoMatchingRootException: 
Matcher 'with decor view of type PopupWindow$PopupViewContainer' 
did not match any of the following roots: 
[Root{application-window-token=android.view.ViewRootImpl$W@bb8371e,
 window-token=android.view.ViewRootImpl$W@bb8371e, has-window-focus=true, 

请问,有人可以帮忙吗?

最佳答案

如果您有 PopupWindow,似乎用 Espresso 不可能进行这种简单的检查。因为 NoMatchingRootException 的方式被抛出

您可能只是捕获异常并完成测试,但这不是一个好的选择,因为抛出和捕获 NoMatchingRootException与默认测试用例相比,会消耗大量时间。似乎 Espresso 正在等待 Root 一段时间

对于这种情况,建议在这里放弃 Espresso 并使用 UiAutomator对于这个断言

val device: UiDevice
   get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun assertPopupIsNotDisplayed() {
    device.waitForIdle()
    assertFalse(device.hasObject(By.text(yourText))))
}

fun assertPopupIsDisplayed() {
    device.waitForIdle()
    assertTrue(device.hasObject(By.text(yourText))))
}

关于android - 如何检查不显示弹出通知。 Espresso ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116938/

相关文章:

android - android中的新菜单?

android - 无法将使用 JVM 目标 1.8 构建的字节码内联到使用 JVM 目标 1.6 构建的字节码

android - 我总是可以在 Kotlin 中将两个条件合并为一行吗?

android - Espresso - 使用应用程序单击菜单项 :showAsAction ="ifRoom"

android - 使用 Glide 在 MenuItem 中加载远程图像

android - 带有 Gradle 错误的 Cordova 6.1.0

android - 如何使用适用于 Android 4.3+ 的 MediaCodec api 对视频进行转码

android - 预期类型不匹配 : inferred type is Any but List<SportNewsResponse>?

android - 用 Espresso 点击不完全可见的 imageButton

java - Jenkins在多种设备上进行Android测试