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

How to check that popup notification is NOT displayed. Espresso

提问人:Stanly T 提问时间:1/10/2019 更新时间:10/2/2020 访问量:1509

问:

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

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
    }

我得到下一个例外:

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, 

拜托,有人能帮忙吗?

Kotlin Android-espresso

评论

0赞 Eduard Kornev 2/19/2020
你找到答案了吗?我有同样的问题

答:

4赞 Beloo 4/24/2020 #1

似乎这种简单的检查是不可能的,如果你有一个,因为扔的方式PopupWindowNoMatchingRootException

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

对于这种情况,建议在这里放弃浓缩咖啡并用于此断言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))))
}

评论

0赞 Hayuki 9/14/2020
感谢您的建议!这是我设法让负面条件起作用的唯一方法。