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

时间:2014-09-17 19:32:04

标签: android navigation-drawer uiautomator android-uiautomator

有没有人能够解决这个问题。 UiScrollable,swipeLeft和swipeRight似乎对它没有任何影响。我正在使用带有最新api的Nexus 5模拟器。有没有人能把它拉下来?

2 个答案:

答案 0 :(得分:3)

TL; DR :使用ActionBarDrawerToggle constructor

中设置的内容说明
  1. 设置导航抽屉时,请设置ActionBarDrawerToggle,其中包含开启和关闭的内容说明。
  2. 
    // 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对象,然后单击它。
    2. 
      // 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()方法不起作用。作为解决方法,您只需选择抽屉菜单中打开的部分;这会关闭抽屉并在打开之前显示相同的部分。

答案 1 :(得分:2)

你可以试试这个:

UiObject view = new UiObject(new UiSelector()."use what you want to identify the view");
Rect bounds = view.getBounds(); // Returns the bounds of the view as (left, top, right, bottom)
int center_x = bounds.centerX();
int center_y = bounds.centerY();
// Now you have the center of the view. You can just slide by using drag()
getUiDevice().drag(center_x, center_y, center_x + "amount of pixels", center_y)
// Where amount of pixels can be a variable set to a fraction of the screen width

我在抽屉上使用了这个想法,该抽屉在文件浏览器应用程序上从左向右滑动。 我没有使用与上面完全相同的代码,因为我使用的是python包装器,用于uiautomator。 (https://github.com/xiaocong/uiautomator