我在尝试编写与listview项交互的代码时遇到AmbiguousViewMatcherException异常。情景如下。
我有一个包含两个视图的列表视图
我在列表中有近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 ****”。
这是我发现的一个例子,希望它有所帮助:
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")));
}
根据错误消息,您有多个视图,其id等于R.id.List
。您应首先检查视图层次结构,并将列表ID(id/List
)替换为您要匹配的视图的唯一ID。
将列表视图放在allOff()中,因为您的ID列表在项目中多次使用。
onView(allOff(withId(R.id.List)))。check(matches(withAdaptedData(withItemContent(“Book it”))));