Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - How to check if one of the view is displayed

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 can check with is Displayed(). But that would fail if other view is visible instead. I want to pass the test if any one of those two views are displayed.

onMyButton.perform(click());  onMyPageOne.check(matches(isDisplayed())); //view 1 or onMyPageTwo.check(matches(isDisplayed())); //view 2 

After, perform click on MyButton, any one of the view (1 or 2) is expected to appear but not both. It is not fixed that which one would be displayed. How can I check if any one of them is displayed?

like image 326
user846316 Avatar asked Mar 25 '15 08:03

user846316


People also ask

How do I know if my espresso view is visible?

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.

How do you assert espresso?

check is a method which accepts an argument of type ViewAssertion and do assertion using passed in ViewAssertion object. matches(withText(“Hello”)) returns a view assertion, which will do the real job of asserting that both actual view (found using withId) and expected view (found using withText) are one and the same.

How do I get Textview espresso from text?

The basic idea is to use a method with an internal ViewAction that retrieves the text in its perform method. Anonymous classes can only access final fields, so we cannot just let it set a local variable of getText() , but instead an array of String is used to get the string out of the ViewAction .


2 Answers

It's possible to catch the exceptions raised by Espresso like this:

If you want to test if a view is in hierarchy:

try {     onView(withText("Button")).perform(click());     // View is in hierarchy  } catch (NoMatchingViewException e) {     // View is not in hierarchy } 

This exception will be thrown if the view is not in the hierarchy.

Sometimes the view can be in the hierarchy, but we need to test if it is displayed, so there is another exception for assertions, like this:

try {     onView(withText("Button")).check(matches(isDisplayed()));     // View is displayed } catch (AssertionFailedError e) {     // View not displayed } 
like image 131
Lafayette Avatar answered Oct 13 '22 09:10

Lafayette


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))); 
like image 44
RyPope Avatar answered Oct 13 '22 09:10

RyPope