Skip to content

Instantly share code, notes, and snippets.

@luisfernandezbr
Created January 18, 2019 19:10
Show Gist options
  • Save luisfernandezbr/a3e3bbf0cb00e9584bf896b379bdafd3 to your computer and use it in GitHub Desktop.
Save luisfernandezbr/a3e3bbf0cb00e9584bf896b379bdafd3 to your computer and use it in GitHub Desktop.
Trick to verify specified position of an view inside your parent using Espresso. Based on https://stackoverflow.com/a/24933287/659272
...
// Solution based on https://stackoverflow.com/a/24933287/659272
@Test
fun someTest() {
val textView = onView(
withId(R.id.editTextToVerifyPosition)
)
textView.check(
matches(
isChildOfAtPositionMatcher(R.id.layoutToVerifyChildPosition, 1)
)
)
}
private fun isChildOfAtPositionMatcher(@IdRes parentId: Int, position: Int): Matcher<View> {
var parentMatcher = withId(parentId)
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with first child view of type parentMatcher")
}
override fun matchesSafely(view: View): Boolean {
if (view.parent !is ViewGroup) {
return parentMatcher.matches(view.parent)
}
val group = view.parent as ViewGroup
return parentMatcher.matches(view.parent) && group.getChildAt(position) == view
}
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment