2016-07-25 23 views

回答

0

我终于找到了答案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); 
     } 
    }; 
}