Espresso 单击具有特定 id 的每个元素
Espresso click on every element with a certain id
我的应用程序中有几个具有相同 ID 的按钮,我希望 Espresso 可以点击所有这些按钮。这些基本上是 collapse/expand 个按钮,因此我
希望 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.
如果您知道有多少个具有相同 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);
}
};
}
获取所有 child 并点击它们。然后,使用它:
onView(nthChildOf(withId(R.id.parent_container), itemNumber)
.perform(click());
循环增加 itemNumber
我的应用程序中有几个具有相同 ID 的按钮,我希望 Espresso 可以点击所有这些按钮。这些基本上是 collapse/expand 个按钮,因此我 希望 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.
如果您知道有多少个具有相同 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);
}
};
}
获取所有 child 并点击它们。然后,使用它:
onView(nthChildOf(withId(R.id.parent_container), itemNumber)
.perform(click());
循环增加 itemNumber