浓缩咖啡如何检查对话框是否不可见



我有一个测试,检查对话框是否存在。

@Test
fun dismissedWhenClicked() {
//dimiss dialog
onView(withText(R.string.simple)).inRoot(isDialog()).perform(click())
//check dialog
onView(isRoot()).inRoot(isDialog()).check(matches(not(isDisplayed())))
}

以上是我的最佳猜测,但由于Matcher 'is dialog' did not match any of the following roots而失败

我在这里找到了3个问题来解决它,但似乎没有一个能解决它。

Espresso检查是否没有显示对话框-注释有效,但当有对话框时也会通过

检查对话框是否可见-Espresso-这并没有检查,相反,我认为它会优雅地失败。

espresso:断言对话没有显示——似乎没有答案。

我用一个从这里稍微修改过的自定义匹配器解决了这个问题

@Test
fun dismissedWhenClicked() {
onView(withText(R.string.simple)).inRoot(isDialog()).perform(click())
onView(withId(R.id.fragment_layout)).inRoot(Utils.ActivityMatcher()).check(matches(isDisplayed()))
}
class ActivityMatcher : TypeSafeMatcher<Root>() {
override fun describeTo(description: Description) {
description.appendText("is activity")
}
public override fun matchesSafely(root: Root): Boolean {
val type: Int = root.windowLayoutParams.get().type
if (type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION) {
val windowToken: IBinder = root.decorView.windowToken
val appToken: IBinder = root.decorView.applicationWindowToken
if (windowToken === appToken) {
//means this window isn't contained by any other windows.
return true
}
}
return false
}
}

最新更新