Based on what I see in Espresso cheat sheet, there are two methods that check visibility of a View, isDisplayed()
and isCompletelyDisplayed()
.
I have sliding layout in my main screen and I have several views in it. I'm checking one of them by following command:
onView(withId(R.id.payment_btn)).check(matches(isDisplayed()));
However, test stops and displays following error:
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
Then I thought that since the View is not visible I can test it by following test:
onView(withId(R.id.payment_btn)).check(doesNotExist());
However, test stopped and displayed following message:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: View is present in the hierarchy:
Expected: is <false>
Got: <true>
So, how can I check visibility of a View off the screen?
When the view is outside the screen, it is not displayed, but it still exist in the view hierarchy. To check that the view is not displayed on the screen use: onView(withId(R.id.payment_btn)).check(matches(not(isDisplayed())));
If you want to check if it is displayed, you have to scroll/swipe to the view, so it becomes visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With