简体   繁体   中英

Robotium UI testing for app with Navigation Drawer

We got the app with Navigation Drawer from support.v4 library. We automating UI testing with Robotium and everything is ok, but Navigation Drawer can freeze randomly so some tests can fail randomly.

This is definitely not a Robotium problem, because I saw how Navigation Drawer gets freezed in some other apps on my device, also in my own apps.

I already tried fix for Navigation Drawer from this question's anwer: Why does DrawerLayout sometimes glitch upon opening?

It helped and random freezes decreased from 90% to about 10%, but 10% of test runs can fail and this is very bad, especially for Continuous Integration...

May be someone already fixed this problem?

I encountered the same problem with our Robotium tests and the solution I ended up going with was to simulate a drag gesture (how a real user would swipe open the drawer) instead of trying to click the drawer toggle or using the solo methods. I seemed to notice the intermittent failues more often on devices running Android older than SDK 18.

I placed this method in our own subclass of Solo and we haven't had a failing test since (over hundreds of runs).

/**
 * Open the navigation drawer with a drag gesture. Click based triggering is
 * flaky on SDK < 18
 */
public void openNavigationDrawer() {
    Point deviceSize = new Point();
    getCurrentActivity().getWindowManager().getDefaultDisplay().getSize(deviceSize);

    int screenWidth = deviceSize.x;
    int screenHeight = deviceSize.y;
    int fromX = 0;
    int toX = screenWidth / 2;
    int fromY = screenHeight / 2;
    int toY = fromY;

    this.drag(fromX, toX, fromY, toY, 1);
}

I am using android.support.v4.widget.DrawerLayout too and didn't find any way to do it simply .

I finally managed to open the drawer by using the code bellow

/**
 * As we use app compat it seems Solo#setNavigationDrawer is not doing well 
 * (drawer does not open, but the button is clicked)
 *
 * Same result for clickOnView(getView(android.R.id.home))
 * 
 * This code opens the navigation drawer on the main thread
 * Be aware : you need to provide your DrawerLayout id (you can do it in params)
 */
 public void openCompatNavigationDrawer() {
     getInstrumentation().runOnMainSync(new Runnable() {
         @Override
         public void run() {
             ((DrawerLayout) mSolo.getView(R.id.drawer_layout))
                  .openDrawer(Gravity.LEFT);
         }
     });
 }

Gist available here https://gist.github.com/quentin7b/9b51a3827c842417636b

Open drawer navigation: solo.clickOnScreen(50, 50);

Choose list item in drawer navigation:

ListView listView = (ListView) solo.getView(R.id.left_drawer); View SwitchOrganizations = listView.getChildAt(0); solo.clickOnView(SwitchOrganizations);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM