Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 525
Hesam Avatar asked Dec 14 '22 08:12

Hesam


1 Answers

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.

like image 191
HowieH Avatar answered Apr 30 '23 15:04

HowieH