In Espresso, how to avoid AmbiguousViewMatcherException when multiple views match

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

    At latest * Run -> Record Espresso Test

    By Click on the Same ID View with different position generate different Code for them so try it.

    Its Actually Resolve these problem.

    0 讨论(0)
  • 2020-11-30 03:35

    You can simply make NthMatcher like:

       class NthMatcher internal constructor(private val id: Int, private val n: Int) : TypeSafeMatcher<View>(View::class.java) {
            companion object {
                var matchCount: Int = 0
            }
            init {
                var matchCount = 0
            }
            private var resources: Resources? = null
            override fun describeTo(description: Description) {
                var idDescription = Integer.toString(id)
                if (resources != null) {
                    try {
                        idDescription = resources!!.getResourceName(id)
                    } catch (e: Resources.NotFoundException) {
                        // No big deal, will just use the int value.
                        idDescription = String.format("%s (resource name not found)", id)
                    }
    
                }
                description.appendText("with id: $idDescription")
            }
    
            public override fun matchesSafely(view: View): Boolean {
                resources = view.resources
                if (id == view.id) {
                    matchCount++
                    if(matchCount == n) {
                        return true
                    }
                }
                return false
            }
        }
    

    Declare like this:

    fun withNthId(resId: Int, n: Int) = CustomMatchers.NthMatcher(resId, n)
    

    And use like this:

    onView(withNthId(R.id.textview, 1)).perform(click())
    
    0 讨论(0)
  • 2020-11-30 03:39

    Cases:

    onView( withId( R.id.songListView ) ).perform( RealmRecyclerViewActions.scrollTo( Matchers.first(Matchers.withTextLabeled( "Love Song"))) );
    onView( Matchers.first(withText( "Love Song")) ).perform( click() );
    

    inside my Matchers.class

    public static Matcher<View> first(Matcher<View> expected ){
    
        return new TypeSafeMatcher<View>() {
            private boolean first = false;
    
            @Override
            protected boolean matchesSafely(View item) {
    
                if( expected.matches(item) && !first ){
                    return first = true;
                }
    
                return false;
            }
    
            @Override
            public void describeTo(Description description) {
                description.appendText("Matcher.first( " + expected.toString() + " )" );
            }
        };
    }
    
    0 讨论(0)
提交回复
热议问题