Created
January 18, 2019 19:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// 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