Вам нужно создать пользовательский Искатель:
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);
}
};
}
Чтобы использовать его:
onView(nthChildOf(withId(R.id.directParentLinearLayout), 0).check(matches(withText("I am the first child"));
В вашем случае вам также необходимо проверить, что является потомком макета, который имеет уникальный идентификатор:
onView(nthChildOf(allOf(withId(R.id.directParentLinearLayout), isDescendantOfA(withId(R.id.linearLayoutWithUniqueId))), 0).check(matches(withText("I am the first child"));