matches(not(isDisplayed())) fails with NoMatchingV

2019-02-02 22:02发布

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!

4条回答
不美不萌又怎样
2楼-- · 2019-02-02 22:42
onView(withText("")).check(doesNotExist());
查看更多
We Are One
3楼-- · 2019-02-02 22:47

Need to use doesNotExist() instead. Found here.

查看更多
We Are One
4楼-- · 2019-02-02 22:47

Also work with yours method, but something like this:

onView(withId(R.id.next)).check(matches(not(isDisplayed())));
查看更多
唯我独甜
5楼-- · 2019-02-02 23:07

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());
查看更多
登录 后发表回答