Skip to content

Instantly share code, notes, and snippets.

@rakib10rr3
Created September 4, 2019 06:52
Show Gist options
  • Save rakib10rr3/522813ad0b7b635977253b64b43f7947 to your computer and use it in GitHub Desktop.
Save rakib10rr3/522813ad0b7b635977253b64b43f7947 to your computer and use it in GitHub Desktop.
//This method selects any child from a viewgroup i.e: Chipgroup
private fun nthChildOf(parentMatcher: Matcher<View>, childPosition: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("position $childPosition of parent ")
parentMatcher.describeTo(description)
}
public override fun matchesSafely(view: View): Boolean {
if (view.parent !is ViewGroup) return false
val parent = view.parent as ViewGroup
return (parentMatcher.matches(parent)
&& parent.childCount > childPosition
&& parent.getChildAt(childPosition) == view)
}
}
}
//Call it like this
onView(nthChildOf(withId(R.id.cgPresent), 0)).perform(click()) // This will click on the first element of chipgroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment