Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-如何使用浓缩咖啡点击导航抽屉上的项目?_Android_Navigation Drawer_Android Espresso - Fatal编程技术网

Android 安卓-如何使用浓缩咖啡点击导航抽屉上的项目?

Android 安卓-如何使用浓缩咖啡点击导航抽屉上的项目?,android,navigation-drawer,android-espresso,Android,Navigation Drawer,Android Espresso,我是Android开发新手。我想用浓缩咖啡来测试我的抽屉是否打开,然后点击一个项目并检查它是否打开了一个新的活动。我一直在寻找这方面的例子,但没有任何运气 @Test public void clickOnYourNavigationItem_ShowsYourScreen() { // Open Drawer to click on navigation. onView(withId(R.id.drawer_layout)) .check(matches(isC

我是Android开发新手。我想用浓缩咖啡来测试我的抽屉是否打开,然后点击一个项目并检查它是否打开了一个新的活动。我一直在寻找这方面的例子,但没有任何运气

@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)));
}
这正是你想要的


其他示例可用,或者

如果其他人遇到此问题,并且他/她正在使用kotlin,您可以在ActivityScenario类上添加扩展功能,以抓取抽屉图标或后退按钮

有关更多说明,请查看此


快乐编码

看起来自定义视图操作中导航到的源代码链接已断开。@SudarsanGP您是对的。似乎它已经被添加到了android sdk中。我将更新答案中的参考。只需使用
android.support.test.espresso.contrib
中定义的
NavigationViewActions
,您从哪里获得
Drawractions
类?请添加导入语句。在
fun <T: Activity> ActivityScenario<T>.getToolbarNavigationContentDescriptor(): String {
    var description = ""
    onActivity {
        description = it.findViewById<Toolbar>(R.id.toolbar).navigationContentDescription as String
    }
    return description
}
// Check drawer is closed
onView(withId(R.id.drawer_layout)).check(matches(isClosed(Gravity.START)))

// Click drawer icon
onView(
    withContentDescription(
        scenario.getToolbarNavigationContentDescriptor()
    )
).perform(click())

// Check if drawer is open
onView(withId(R.id.drawer_layout)).check(matches(isOpen(Gravity.START)))