android-test
android-test copied to clipboard
[FR] Assert an item is present or not in RecyclerView.
Description
I'm looking for an easy way to assert whether an element is present or not in RecyclerView, without having to scroll (the element might or might not be currently on screen). I found a workaround was to assert that clicking the item throws an exception:
Assert.assertThrows(PerformException.class, () -> onView(recyclerView)
.perform(RecyclerViewActions.actionOnItem(matcher, click())));
But I'd prefer to write something like the following instead, which is more precise and direct:
onView(recyclerView).perform(RecyclerViewActions.hasItemMatching(matcher)));
See also this Medium post where the author implements a RecyclerViewAssertions class along these lines.