Tim Boland
Tim Boland

Reputation: 1972

Is it possible to slide open/closed a navigation drawer in uiautomator

Has anyone been able to pull this off. UiScrollable, swipeLeft and swipeRight do not appear to have any affect on it. I am using a Nexus 5 emulator with the latest api. Has anyone been able to pull it off?

Upvotes: 2

Views: 1916

Answers (2)

Christian García
Christian García

Reputation: 4009

TL;DR: Use the content descriptions set in the ActionBarDrawerToggle constructor


  1. When you set up your Navigation Drawer, set an ActionBarDrawerToggle with content descriptions for opening and closing.

// 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. In your UiAutomatorTestCase, to open/close the drawer, find the UI object by the open/close content description defined in step 1 and perform a click on it.

// 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));
}

Warning: If you use the Material design approach for the navigation drawer (where the drawer opens on top of the Topbar and behind the status bar), the "hamburger" icon will be drawn behind the drawer, making the closeDrawer() method not to work. As a workaround, you can just select the section that is open in the drawer menu; this closes the drawer and shows the same section that was there before opening it.

Upvotes: 4

Gabriel Porumb
Gabriel Porumb

Reputation: 1691

You could try this:

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

I used this idea on a drawer that slides from left to right on a file browser app. I did not use the exact same code as above because I'm using a python wrapper for uiautomator. (https://github.com/xiaocong/uiautomator)

Upvotes: 2

Related Questions