Android - 如何使用Espresso点击导航栏上的项目?

jos*_*nag 15 android navigation-drawer android-espresso

我是Android开发的新手.我想使用Espresso来测试我的抽屉是否打开,然后点击一个项目并检查它是否会打开一个新活动.我一直在寻找这方面的例子,但没有运气.

GVi*_*i82 33

@Test
public void clickOnYourNavigationItem_ShowsYourScreen() {
    // Open Drawer to click on navigation.
    onView(withId(R.id.drawer_layout))
        .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
        .perform(DrawerActions.open()); // Open Drawer

    // Start the screen of your activity.
    onView(withId(R.id.nav_view))
        .perform(NavigationViewActions.navigateTo(R.id.your_navigation_menu_item));

    // Check that you Activity was opened.
    String expectedNoStatisticsText = InstrumentationRegistry.getTargetContext()
        .getString(R.string.no_item_available);
    onView(withId(R.id.no_statistics)).check(matches(withText(expectedNoStatisticsText)));
}
Run Code Online (Sandbox Code Playgroud)

这正是您正在寻找的.

其他示例可在此处此处获得