如果我需要检查是否显示了烤面包,则使用此:
viewWithTextResId(server_error).inRoot(ToastMatcher())
.checkDisplayed()
fun ViewInteraction.checkDisplayed(): ViewInteraction {
return check(matches(isDisplayed()))
}
class ToastMatcher : TypeSafeMatcher<Root>() {
override fun describeTo(description: Description) {
description.appendText("is toast")
}
override fun matchesSafely(root: Root): Boolean {
val type = root.getWindowLayoutParams().get().type
if (type == WindowManager.LayoutParams.TYPE_TOAST) {
val windowToken = root.getDecorView().getWindowToken()
val appToken = root.getDecorView().getApplicationWindowToken()
if (windowToken === appToken) {
return true
}
}
return false
}
}
很好,工作正常。 但是我需要测试,如果吐司是show / hide,则返回boolean。