jueves, 18 de abril de 2019

Android: Espresso: Start testing from not-main activity

@Rule@JvmFieldvar mActivityTestRule = ActivityTestRule(<some_activity>::class.java)

Android: Espresso: Recycler view scroll to position

onView(withId(R.id.profile_recycler))
        .perform(scrollToPosition<RecyclerObjectHolder>(5))

Android: Espresso: Press ime action button

onView(withId(R.id.button)).perform(pressImeActionButton())

Android: Espresso: Grab button in row with sibling text

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

R.id.options is the id of the button
testAddress is a val with the text of the associated (sibling) text view

Android: Espresso: Child at position

fun childAtPosition(
        parentMatcher: Matcher<View>, position: Int): Matcher<View> {

    return object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description) {
            description.appendText("Child at position $position in parent ")
            parentMatcher.describeTo(description)
        }

        public override fun matchesSafely(view: View): Boolean {
            val parent = view.parent
            return parent is ViewGroup && parentMatcher.matches(parent)
                    && view == parent.getChildAt(position)
        }
    }
}

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
        }
    }
}

Android: Espresso: First element of selection

fun <T> first(matcher: Matcher<T>): Matcher<T> {
    return object : BaseMatcher<T>() {
        internal var isFirst = true
        override fun matches(item: Any): Boolean {
            if (isFirst && matcher.matches(item)) {
                isFirst = false
                return true
            }

            return false
        }

        override fun describeTo(description: Description) {
            description.appendText("should return first matching item")
        }
    }
}

Android: Espresso: Getting view from popup view

Example:

onView(withText("Delete"))
        .inRoot(isPlatformPopup()) // Key line
        .perform(click())

onView(withText("Ok"))
        .inRoot(isDialog()) // Key line
        .perform(click())