Espresso AmbiguousViewMatcherException

问题描述 投票:0回答:3

我在尝试编写与listview项交互的代码时遇到AmbiguousViewMatcherException异常。情景如下。

我有一个包含两个视图的列表视图

  1. 的TextView
  2. buttonView

我在列表中有近250行。所有按钮都有文本“预订”或“已取消”。他们正在洗牌。我想指示Espresso点击列表中的第一个“预订”按钮。我已经尝试了很多场景,仍然无法解决这个问题。请有人帮助我。

以下是我的代码

onView(withId(R.id.List))
                .check(matches(withAdaptedData(withItemContent("Book it"))));

////////////////////////////////////////////////////////

private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("with class name: ");
                dataMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                if (!(view instanceof AdapterView)) {
                    return false;
                }
                @SuppressWarnings("rawtypes")
                Adapter adapter = ((AdapterView) view).getAdapter();
                for (int i = 0; i < adapter.getCount(); i++) {
                    if (dataMatcher.matches(adapter.getItem(i))) {
                        return true;
                    }
                }
                return false;
            }
        };
    }

////////////////////////////////////////////////////////////

android.support.test.espresso.AmbiguousViewMatcherException:'with id:com.bottegasol.com.migym.EmpireSportFit:id / List'匹配层次结构中的多个视图。问题视图在下方标有“**** MATCHES ****”。

android listview android-espresso
3个回答
0
投票

这是我发现的一个例子,希望它有所帮助:

public void testClickOnFirstAndFifthItemOfLength8() {
    onData(is(withItemSize(8)))
      .atPosition(0)
      .perform(click());
    onView(withId(R.id.selection_row_value))
      .check(matches(withText("10")));
    onData(is(withItemSize(8)))
      .atPosition(4)
      .perform(click());
    onView(withId(R.id.selection_row_value))
      .check(matches(withText("14")));
  }

来源:https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/androidTest/java/android/support/test/testapp/AdapterViewTest.java


0
投票

根据错误消息,您有多个视图,其id等于R.id.List。您应首先检查视图层次结构,并将列表ID(id/List)替换为您要匹配的视图的唯一ID。


0
投票

将列表视图放在allOff()中,因为您的ID列表在项目中多次使用。

onView(allOff(withId(R.id.List)))。check(matches(withAdaptedData(withItemContent(“Book it”))));

© www.soinside.com 2019 - 2024. All rights reserved.