Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 359
user846316 Avatar asked Feb 10 '15 12:02

user846316


3 Answers

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.

like image 69
user846316 Avatar answered Nov 04 '22 07:11

user846316


Also work with yours method, but something like this:

onView(withId(R.id.next)).check(matches(not(isDisplayed())));
like image 31
Morozov Avatar answered Nov 04 '22 08:11

Morozov


onView(withText("")).check(doesNotExist());
like image 21
Olivia Liao Avatar answered Nov 04 '22 07:11

Olivia Liao