android - 是否可以在 uiautomator 中滑动打开/关闭抽屉导航

标签 android navigation-drawer android-uiautomator

有没有人能做到这一点。 UiScrollable、swipeLeft 和 swipeRight 似乎对其没有任何影响。我正在使用带有最新 api 的 Nexus 5 模拟器。有人成功过吗?

最佳答案

TL;DR:使用 ActionBarDrawerToggle constructor 中设置的内容描述


  1. 设置抽屉导航时,设置一个 ActionBarDrawerToggle,其中包含用于打开和关闭的内容说明。

// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";

// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";

private void setUpNavigationDrawer() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, CONTENT_DESCRIPTION_OPEN_DRAWER, CONTENT_DESCRIPTION_CLOSE_DRAWER);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}
  1. 在您的 UiAutomatorTestCase 中,要打开/关闭抽屉,请根据第 1 步中定义的打开/关闭内容描述找到 UI 对象,然后单击它。

// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";

// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";

private void openNavDrawer() throws UiObjectNotFoundException {
    findViewByContentDescription(CONTENT_DESCRIPTION_OPEN_DRAWER).click();
}
private void closeNavDrawer() throws UiObjectNotFoundException {
    findViewByContentDescription(CONTENT_DESCRIPTION_CLOSE_DRAWER).click();
}
private UiObject findViewByContentDescription(String description) {
    return new UiObject(new UiSelector().description(description));
}

警告:如果您使用 Material design approach对于抽屉导航(抽屉在 Topbar 顶部和状态栏后面打开),“汉堡包”图标将绘制在抽屉后面,使 closeDrawer() 方法不起作用。作为解决方法,您可以只选择在抽屉菜单中打开的部分;这将关闭抽屉并显示打开它之前的相同部分。

关于android - 是否可以在 uiautomator 中滑动打开/关闭抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898852/

相关文章:

Android Sqlite 性能

android - 如何访问有状态小部件列表的状态对象的方法? ( flutter )

android - 是否可以使用带有导航绘制的 fragment 并在操作栏中显示图标

java - UIautomator - Android Studio - 如何生成 APK 或 JAR 文件

Android:子进程(logcat)在父进程(app)死后继续运行

android - 如何停止在多个 Activity 中使用的抽屉导航 Activity 中选中项目的点击事件

android - NavigationDrawer 菜单中的 ScrollView

android - UiAutomator 选择应用程序以从应用程序抽屉中进行测试

android - 如何检测 uiautomator 中的抬头通知?

java - 安卓/MySQL : Populate Spinner from database with Initial Set Text "Please Select"