jueves, 18 de abril de 2019

Android: Espresso: Scrolling to element in recycler view

// Do scroll
onView(withId(R.id.profile_recycler))
        .perform(scrollToHolder(first(allOf(line1WithText(testAddress))))) (1)

// Had to do it twice so it will actually display it whole...(2)
onView(withId(R.id.profile_recycler))
        .perform(scrollToHolder(first(allOf(line1WithText(testAddress)))))

onView(first(allOf(hasSibling(withText(testAddress)), withId(R.id.options))))
        .perform(click())

(1) You probably will have to add:

androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.2.2') {
    exclude group: 'com.android.support', module: 'appcompat'    exclude group: 'com.android.support', module: 'support-v4'    exclude module: 'recyclerview-v7'}

in your gradle file yo be able to use 'scrollToHolder'





(2) For some reason the scroll wouldn't only scroll to the tip of the row and 
therefore wasn't able to work with that view further.



Note:


1) first is defined here
2) line1WithText its a matcher use to match a row in my recycler view:



fun line1WithText(text: String): TypeSafeMatcher<RecyclerObjectHolder> {
    return object: TypeSafeMatcher<RecyclerObjectHolder>() {
        override fun describeTo(description: Description?) {
            description?.appendText("Scroll to holder")
        }

        override fun matchesSafely(item: RecyclerObjectHolder?): Boolean {

            val title = item?.row1?.text?.toString()

            if (title != null && title == text) {
                return true
            }

            return false
        }
    }
}

No hay comentarios:

Publicar un comentario