Android - 如何使用 Espresso 单击导航抽屉上的项目?
Android - How to click on an item on a navigation drawer using Espresso?
我是 Android 开发的新手。我想使用 Espresso 来测试我的抽屉是否打开,然后单击一个项目并检查它是否打开了一个新的 activity。我一直在寻找这方面的例子,但一直没有成功。
@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)));
}
这正是您要寻找的。
万一其他人回答了这个问题并且 he/she 正在使用 kotlin,您可以在 ActivityScenario class 上添加扩展功能以获取抽屉图标或后退按钮
有关更多说明,请查看此 GOOGLE-TESTING-CODELAB-8
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)))
编码愉快。 . .
我是 Android 开发的新手。我想使用 Espresso 来测试我的抽屉是否打开,然后单击一个项目并检查它是否打开了一个新的 activity。我一直在寻找这方面的例子,但一直没有成功。
@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)));
}
这正是您要寻找的。
万一其他人回答了这个问题并且 he/she 正在使用 kotlin,您可以在 ActivityScenario class 上添加扩展功能以获取抽屉图标或后退按钮
有关更多说明,请查看此 GOOGLE-TESTING-CODELAB-8
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)))
编码愉快。 . .