如何检查是否不显示弹出通知。浓咖啡

Sta*_*y T 5 android kotlin android-espresso

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

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

我对第二个测试用例有问题,我必须检查我的弹出通知是否已经消失(意味着它不再显示)。所以我尝试使用下一个方法:

    fun viewInNotificationPopupIsNotDisplayed(viewMatcher: Matcher<View>) {
        Espresso.onView(viewMatcher)
            .inRoot(RootMatchers.isPlatformPopup())
            .check(matches(not(isDisplayed())))
          //.check(ViewAssertions.doesNotExist())  // doesn't work as well
    }
Run Code Online (Sandbox Code Playgroud)

我得到下一个异常:

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, 
Run Code Online (Sandbox Code Playgroud)

请问有人可以帮忙吗?

Bel*_*loo 4

如果你有一个浓缩咖啡,那么这种简单的检查似乎是不可能的,因为它的抛出PopupWindow方式NoMatchingRootException

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

对于这种情况,建议放弃这里的浓缩咖啡并用于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))))
}
Run Code Online (Sandbox Code Playgroud)