【问题标题】:How to check that popup notification is NOT displayed. Espresso如何检查不显示弹出通知。浓咖啡
【发布时间】:2019-01-09 19:22:14
【问题描述】:

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

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, 

请问有人可以帮忙吗?

【问题讨论】:

  • 你找到答案了吗?我有同样的问题

标签: android kotlin android-espresso


【解决方案1】:

如果您有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))))
}

【讨论】:

  • 感谢您的建议!这是我设法让负面条件发挥作用的唯一方法。
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多