如何使用 Espresso 检查屏幕外视图的可见性?

How to check visibility of View that is off the screen with Espresso?

根据我在 Espresso cheat sheet 中看到的内容,有两种方法可以检查视图的可见性,isDisplayed()isCompletelyDisplayed()

我的主屏幕有滑动布局,其中有多个视图。我正在通过以下命令检查其中之一:

onView(withId(R.id.payment_btn)).check(matches(isDisplayed()));

但是,测试停止并显示以下错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user

然后我想既然View是不可见的我可以通过以下测试来测试它:

onView(withId(R.id.payment_btn)).check(doesNotExist());

但是,测试停止并显示以下消息:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: View is present in the hierarchy: 
Expected: is <false>
Got: <true> 

那么,如何检查屏幕外视图的可见性?

当视图在屏幕外时,不显示,但仍然存在于视图层级中。要检查视图是否未显示在屏幕上,请使用:onView(withId(R.id.payment_btn)).check(matches(not(isDisplayed())));

如果你想检查它是否显示,你必须 scroll/swipe 到视图,所以它变得可见。