Testing RecyclerView if it has data with Espresso
There are a little bit different scenarios in question and in the comment.
Let's implement the next test scenario: If recycler view does not contain anything, do nothing. If recycler view has at least one element, click on the first.
@Rule
public final ActivityTestRule<YourActivity> mActivityRule = new ActivityTestRule<>(YourActivity.class);
@Test
public void testSample(){
if (getRVcount() > 0){
onView(withId(R.id.our_recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
}
}
private int getRVcount(){
RecyclerView recyclerView = (RecyclerView) mActivityRule.getActivity().findViewById(R.id.our_recycler_view);
return recyclerView.getAdapter().getItemCount();
}