Espresso:如何通过索引获取视图的子项
Espresso: how to get the children of a View by index
我有一个包含 EditText 的视图,我想确保它们以正确的顺序显示。我使用 Espresso,但我不知道如何通过索引获取视图的子项。这是一种方法吗?
我终于找到了答案here。
这个想法是创建一个新的匹配器。
这是更好的方法吗,直接使用 Espresso 而无需创建匹配器?
public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("position " + childPosition + " of parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
if (!(view.getParent() instanceof ViewGroup)) return false;
ViewGroup parent = (ViewGroup) view.getParent();
return parentMatcher.matches(parent)
&& parent.getChildCount() > childPosition
&& parent.getChildAt(childPosition).equals(view);
}
};
}
我有一个包含 EditText 的视图,我想确保它们以正确的顺序显示。我使用 Espresso,但我不知道如何通过索引获取视图的子项。这是一种方法吗?
我终于找到了答案here。 这个想法是创建一个新的匹配器。 这是更好的方法吗,直接使用 Espresso 而无需创建匹配器?
public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("position " + childPosition + " of parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
if (!(view.getParent() instanceof ViewGroup)) return false;
ViewGroup parent = (ViewGroup) view.getParent();
return parentMatcher.matches(parent)
&& parent.getChildCount() > childPosition
&& parent.getChildAt(childPosition).equals(view);
}
};
}