我运行Espresso测试只是为了测试RecyclerView中视图的点击以及RecyclerView视图中的文本。
该测试通过了我的Genymotion模拟器,但在我的设备(Google Nexus 6)上失败了,并且我无法找出原因:
@RunWith(AndroidJUnit4.class)
public class EspressoTest {
@Rule
public ActivityTestRule<MainActivity> firstRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void testRecyclerViewClick() {
Espresso.onView(ViewMatchers.withId(R.id.recycler_view_ingredients)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
}
//TODO: This test is not passing on my device, but it is passing on an emulator. I am not sure why, but have read about idling resources. Can you confirm how to handle?
@Test
public void testRecyclerViewText(){
Espresso.onView(ViewMatchers.withId(R.id.recycler_view_ingredients)).perform(RecyclerViewActions.scrollToPosition(0));
Espresso.onView(withText("Nutella Pie")).check(matches(isDisplayed()));
}
}