我有一个ListView,它有多个Linearlayout。(1) LinearLayout-> FrameLayout->使用-> ImageView(2) LinearLayout-> FrameLayout->使用-> ImageView
如何计数ImageView的个数?它们都有相同的id
您可以使用自定义匹配器-公共最终类CustomMatchers {
public static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
在您的测试用例中使用它-
ViewInteraction textView9 = onView(
allOf(withText("ViewName"),
CustomMatchers.childAtPosition(
CustomMatchers.childAtPosition(withId(R.id.view)id), 1), 0), isDisplayed()))