我的应用程序中有几个按钮具有相同的ID,我希望Espresso点击所有这些按钮。这些基本上是折叠/展开按钮,因此我 希望UI测试扩展视图中的所有元素。
我总是得到:
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.myapp.android:id/liveLinearLayout' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
编辑:根据回复尝试了这个:
onView(allOf(withId(R.id.footage_layout_expand_group))).perform(click());
再次得到这个:
android.support.test.espresso.AmbiguousViewMatcherException: '(with id: com.myapp.android:id/footage_layout_expand_group)' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
答案 0 :(得分:4)
如果您知道有多少具有相同ID的项目,您可以使用:
public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
return new TypeSafeMatcher<View>() {
@Override public void describeTo(Description description) {
description.appendText("with "+childPosition+" child view of type parentMatcher");
}
@Override public boolean matchesSafely(View view) {
if (!(view.getParent() instanceof ViewGroup)) {
return parentMatcher.matches(view.getParent());
}
ViewGroup group = (ViewGroup) view.getParent();
return parentMatcher.matches(view.getParent()) && group.getChildAt(childPosition).equals(view);
}
};
}
对孩子进行getevery并点击它们。然后,使用它:
onView(nthChildOf(withId(R.id.parent_container), itemNumber)
.perform(click());
在循环中增加itemNumber