Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso check view either doesNotExist or not isDisplayed

The following statement does not work because doesNotExist() returns a ViewAssertion instead of a matcher. Any way to make it work without a try-catch?

.check(either(matches(doesNotExist())).or(matches(not(isDisplayed()))));
like image 560
ferbeb Avatar asked Dec 23 '16 08:12

ferbeb


2 Answers

If you want to check if a view doesn't not exist in the hierarchy, please use below assertion.

ViewInteraction.check(doesNotExist());

If you want to check if a view exist in the hierarchy but not displayed to the user, please use below assertion.

ViewInteraction.check(matches(not(isDisplayed())));

Hope this helps.

like image 169
Sivakumar Kamichetty Avatar answered Oct 13 '22 01:10

Sivakumar Kamichetty


I had the same issue, one of my views will not have a certain view initially but could add it and hide it later. Which state the UI was in depended on wether background activities were destroyed.

I ended up just writing a variation on the implementation of doesNotExist:

public class ViewAssertions {
    public static ViewAssertion doesNotExistOrGone() {
        return new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noView) {
                if (view != null && view.getVisibility() != View.GONE) {
                    assertThat("View is present in the hierarchy and not GONE: "
                               + HumanReadables.describe(view), true, is(false));
                }
            }
        };
    }
}
like image 28
user2143491 Avatar answered Oct 13 '22 01:10

user2143491