In Espresso, how to avoid AmbiguousViewMatcherException when multiple views match

前端 未结 9 1476
情深已故
情深已故 2020-11-30 03:16

Having gridView which has some images. The gridView\'s cell comes out from same predefined layout, which has same id and desc.

R.id.item_image == 2131

9条回答
  •  执念已碎
    2020-11-30 03:27

    I created a ViewMatcher which matches the first view it finds. Maybe it is helpful for somebody. E.g. when you don't have an AdapterView to use onData() on.

    /**
     * Created by stost on 15.05.14.
     * Matches any view. But only on first match()-call.
     */
    public class FirstViewMatcher extends BaseMatcher {
    
    
       public static boolean matchedBefore = false;
    
       public FirstViewMatcher() {
           matchedBefore = false;
       }
    
       @Override
       public boolean matches(Object o) {
           if (matchedBefore) {
               return false;
           } else {
               matchedBefore = true;
               return true;
           }
       }
    
       @Override
       public void describeTo(Description description) {
           description.appendText(" is the first view that comes along ");
       }
    
       @Factory
       public static  Matcher firstView() {
           return new FirstViewMatcher();
       }
    }
    

    Use it like this:

     onView(FirstViewMatcher.firstView()).perform(click());
    

提交回复
热议问题