In Espresso, how to avoid AmbiguousViewMatcherException when multiple views match

前端 未结 9 1451
情深已故
情深已故 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:24

    Tried @FrostRocket answer as was looking most promissing but needed to add some customisations:

    public static Matcher withIndex(final Matcher matcher, final int index) {
        return new TypeSafeMatcher() {
            int currentIndex;
            int viewObjHash;
    
            @SuppressLint("DefaultLocale") @Override
            public void describeTo(Description description) {
                description.appendText(String.format("with index: %d ", index));
                matcher.describeTo(description);
            }
    
            @Override
            public boolean matchesSafely(View view) {
                if (matcher.matches(view) && currentIndex++ == index) {
                    viewObjHash = view.hashCode();
                }
                return view.hashCode() == viewObjHash;
            }
        };
    }
    

提交回复
热议问题