I have a view containing EditText and I want to be sure that they are displayed in the correct order. I use Espresso but I don't know how to get the children of a view by index. Is it a way to do that?
Asked
Active
Viewed 2,300 times
1 Answers
0
I have finally found an answer here. The idea is to create a new Matcher. Is it a better way to do that, directly with Espresso without to create a Matcher?
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);
}
};
}