In my test, after one action, there are two possible views which can appear and both of them are correct. How can I check if one of the view is displayed. For a single view I ca
One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.
Another way but more accurate (I have not tried) is to check for the rectangular bounds of the View. Not so simple.
Is that clear enough? cannot give you specific examples since you did not post code.
I researched Espresso a bit, and I found this @ Espresso Samples.
Search text "Asserting that a view is not displayed". It says "The above approach works if the view is still part of the hierarchy." So I think your code should work but you need to use ViewAssertions
also. Using your code, perhaps do this:
if (ViewAssertions.doesNotExist()) == null) {
return;
}
onMyPageOne.check(matches(isDisplayed()));
Another technique is check for UI existence. Search for text "Asserting that a view is not present". Using your code, my best suggestion is:
onMyPageOne.check(doesNotExist());
Note: This calls doesNotExist
method.
Their sample code is: onView(withId(R.id.bottom_left)).check(doesNotExist());
There are two cases here that you could be trying to cover. The first is if you are checking if the view "is displayed on the screen to the user" in which case you would use isDisplayed()
onView(matcher).check(matches(isDisplayed()));
or the negation
onView(matcher).check(matches(not(isDisplayed())));
The other case is if you are checking if the view is visible but not necessarily displayed on the screen (ie. an item in a scrollview). For this you can use withEffectiveVisibility(Visibility)
onView(matcher).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
The problem is that all assertoin()
and check()
methods return Assertion
that stops test flow if failed.