Having gridView which has some images. The gridView\'s cell comes out from same predefined layout, which has same id and desc.
R.id.item_image == 2131
You can simply make NthMatcher like:
class NthMatcher internal constructor(private val id: Int, private val n: Int) : TypeSafeMatcher(View::class.java) {
companion object {
var matchCount: Int = 0
}
init {
var matchCount = 0
}
private var resources: Resources? = null
override fun describeTo(description: Description) {
var idDescription = Integer.toString(id)
if (resources != null) {
try {
idDescription = resources!!.getResourceName(id)
} catch (e: Resources.NotFoundException) {
// No big deal, will just use the int value.
idDescription = String.format("%s (resource name not found)", id)
}
}
description.appendText("with id: $idDescription")
}
public override fun matchesSafely(view: View): Boolean {
resources = view.resources
if (id == view.id) {
matchCount++
if(matchCount == n) {
return true
}
}
return false
}
}
Declare like this:
fun withNthId(resId: Int, n: Int) = CustomMatchers.NthMatcher(resId, n)
And use like this:
onView(withNthId(R.id.textview, 1)).perform(click())