How can I check if my recyclerview is empty using espresso?
I know I can check if it has elements by clicking on element position but I want start my test by checking if it is empty.
@RunWith(AndroidJUnit4ClassRunner::class)
class TagsFragmentTest {
@Rule
@JvmField
var repeatRule: RepeatRule = RepeatRule()
@get:Rule
var activityScenarioRule = activityScenarioRule<LoginActivity>()
@Test
@RepeatTest(1)
fun test_checkTags() {
//check main activity
Thread.sleep(5000);
onView(withId(R.id.mainFragmentManager)).check(matches(isDisplayed()))
//go to Tags fragment
onView(withId(R.id.nav_tags)).check(matches(isDisplayed()))
onView(withId(R.id.nav_tags)).perform(click())
//CHECK MY RECYCLERVIEW IS EMPTY
//ADD TAGS
//CHECK IF HAS TAGS
}
}
I created a custom ViewMatcher:
fun recyclerViewSizeMatcher(matcherSize: Int): Matcher<View?>? {
return object : BoundedMatcher<View?, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("with list size: $matcherSize")
}
override fun matchesSafely(recyclerView: RecyclerView): Boolean {
return matcherSize == recyclerView.adapter!!.itemCount
}
}
}
that checks if the adapter.itemCount
has the passed elements.
You could use it as:
recyclerViewSizeMatcher(0)
in that case it has to be empty (0 elements).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With