matches(not(isDisplayed())) fails with NoMatchingViewException

Android Espresso

Android Espresso Problem Overview


I am trying to test the absence of the UI view. The view selector is as follows:

public static ViewInteraction onMyTestUi() {
    return onView(withId(R.id.myTestId));
}

The selector works fine to check if the view is displayed, but gives error on checking if the view not displayed. I am using this as follows:

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

But I get the following error:

> com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: > No views in hierarchy found matching: with id: is If > the target view is not part of the view hierarchy, you may need to use > Espresso.onData to load it from one of the following > AdapterViews:android.widget.ListView{...}

This is strange. I am checking the absence of the UI and its expected that this view won't be found. Then why Espresso is throwing error? Please suggest what might be going wrong here.

Thanks, Amazed!

Android Espresso Solutions


Solution 1 - Android Espresso

Need to use doesNotExist() instead. Found here.

If the view is there in the view hierarchy but in an invisible state (visibility is set to 'INVISIBLE'), use not(isDisplayed). However, if the view is not there at all in the view hierarchy (e.g. visibility set to 'GONE'), doesNotExist() is used.

Solution 2 - Android Espresso

Also work with yours method, but something like this:

onView(withId(R.id.next)).check(matches(not(isDisplayed())));

Solution 3 - Android Espresso

onView(withText("")).check(doesNotExist());

Solution 4 - Android Espresso

You can try this option if you check the view visibility "withEffectiveVisibility"

    onView(withId(R.id.YOURVIEW)).check(matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.GONE)))

Solution 5 - Android Espresso

If you want to check if View is either not visible or does not exist.

public static ViewAssertion isNotDisplayed() {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noView) {
            if (view != null && isDisplayed().matches(view)) {
                throw new AssertionError("View is present in the hierarchy and Displayed: "
                        + HumanReadables.describe(view));
            }
        }
    };
}

Usage:

onView(withId(R.id.someView)).check(isNotDisplayed());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser846316View Question on Stackoverflow
Solution 1 - Android Espressouser846316View Answer on Stackoverflow
Solution 2 - Android EspressoMorozovView Answer on Stackoverflow
Solution 3 - Android EspressoOlivia LiaoView Answer on Stackoverflow
Solution 4 - Android EspressoPabelView Answer on Stackoverflow
Solution 5 - Android Espressojimmy0251View Answer on Stackoverflow