я бы предложил следующий вариант, реализовать метод в классе:
fun checkTextOnPosition(position: Int, text: String) {
onView(withRecyclerView(R.id.recyclerView).atPosition(position))
.check(ViewAssertions.matches(hasDescendant(withText(text))))
}
Где будете просто в зависимости от нужных вам параметров вызывать данный метод, задавая позицию и текст которую хотите проверить.
Дополнительно придётся реализовать метод:
fun withRecyclerView(recyclerViewId: Int): RecyclerViewMatcher {
return RecyclerViewMatcher(recyclerViewId)
}
А вместе с ним и класс:
class RecyclerViewMatcher(private val recyclerViewId: Int) {
fun atPosition(position: Int): Matcher<View> {
return atPositionOnView(position, -1)
}
fun atPositionOnView(position: Int, targetViewId: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
internal var resources: Resources? = null
internal var childView: View? = null
override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)", recyclerViewId)
}
}
description.appendText("RecyclerView with id: $idDescription at position: $position")
}
public override fun matchesSafely(view: View): Boolean {
this.resources = view.resources
if (childView == null) {
val recyclerView = view.rootView.findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView.id == recyclerViewId) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
if (viewHolder != null) {
childView = viewHolder.itemView
}
} else {
return false
}
}
if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById(targetViewId)
return view === targetView
}
}
}
}
}
Также дополнительно если нужно прокликивать по элементам:
fun clickOnTextInPosition(position: Int, title: String) {
Espresso.onView(ViewMatchers.withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition<CurrencySelectorItemHolder>(position, ViewActions.click()))
}