In espresso i am trying to access 3rd child element of linear layout. see attached screenshot.
i tried some matcher, but not get it working.
In espresso i am trying to access 3rd child element of linear layout. see attached screenshot.
i tried some matcher, but not get it working.
You need to create a custom 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("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);
}
};
}
To use it:
onView(nthChildOf(withId(R.id.directParentLinearLayout), 0).check(matches(withText("I am the first child"));
In your case you also need to check that is descendant of the layout that has the unique id:
onView(nthChildOf(allOf(withId(R.id.directParentLinearLayout), isDescendantOfA(withId(R.id.linearLayoutWithUniqueId))), 0).check(matches(withText("I am the first child"));