Issue
I have a listview that I am testing and need to count the number of rows in the list.
How Can this be done?
Solution
You can assert the number of element in your RecyclerListView or ListView by using a custom Matcher like this :
/**
* Test if a RecyclerView contain the right amount of elements
*/
fun withListSize(size: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
var listSize = 0
public override fun matchesSafely(view: View): Boolean {
if ((view !is RecyclerView)) throw IllegalStateException("You cannot assert withListSize in none RecyclerView View")
listSize = view.adapter.itemCount
return listSize == size
}
override fun describeTo(description: Description) {
description.appendText("ListView should have $size items and currently $listSize items")
}
}
}
And use it like this :
var listSize = 0
Espresso.onView(ViewMatchers.withId(R.id.list))
.check(ViewAssertions.matches(object : TypeSafeMatcher<View>() {
public override fun matchesSafely(view: View): Boolean {
if ((view !is RecyclerView)) throw IllegalStateException("You cannot assert withListSize in none RecyclerView View")
listSize = view.adapter.itemCount
return listSize !=0
}
override fun describeTo(description: Description) {
description.appendText("ListView should not be empty")
}
}))
// ADD TO the list
Espresso.onView(ViewMatchers.withId(R.id.list))
.check(ViewAssertions.matches(Matchers.allOf(withListSize(listSize + 1), ViewMatchers.isDisplayed())))
Answered By - Timo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.