matches(not(isDisplayed()))与NoMatchingViewException失败

时间:2015-02-10 12:24:49

标签: android-espresso

我正在尝试测试UI视图的缺失。视图选择器如下:

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

选择器可以正常检查视图是否显示,但在检查视图是否显示时出错。我使用如下:

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

但是我收到以下错误:

  

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException:   层次结构中没有查看匹配的视图:id:is If   目标视图不是视图层次结构的一部分,您可能需要使用   Espresso.onData从以下之一加载它   AdapterViews:android.widget.ListView {...}

这很奇怪。我正在检查用户界面是否缺席,并且预计此视图无法找到。那为什么Espresso会抛出错误呢? 请在这里建议可能出现的问题。

谢谢, 惊讶!

5 个答案:

答案 0 :(得分:123)

需要使用doesNotExist()。 找到here

答案 1 :(得分:12)

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

答案 2 :(得分:10)

也可以使用你的方法,但是这样的话:

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

答案 3 :(得分:5)

如果您想检查View是否不可见不存在

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));
            }
        }
    };
}

用法:

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

答案 4 :(得分:0)

如果您查看视图可见性“ withEffectiveVisibility”,则可以尝试使用此选项

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