我有弹出通知。我的第一个测试检查该通知是否显示并成功通过。我对此测试使用下一个方法:
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/