19

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?

Community
  • 1
  • 1
Artem Zinnatullin
  • 4,305
  • 1
  • 29
  • 43
  • It's been a while since you asked this, did you find any way of dealing with this problem? – Mendhak Apr 12 '14 at 00:21
  • 1
    Nope, but our QA engineer suggested to try to open Navigation Drawer in loop and check visibility of its items and if ok, break the loop. You can try this approach – Artem Zinnatullin Apr 12 '14 at 20:48
  • Sounds good. I've also been playing with `solo.setNavigationDrawer(Solo.OPENED);` which showed up in Robotium 5.1. and `solo.sendKey(Solo.MENU);` several times (I've wired the menu to open the drawer). I'll try your suggestion, it sounds a lot simpler. – Mendhak Apr 12 '14 at 22:02
  • Are you testing it on a emulator. Try running on a actual device. – Sagar Waghmare May 31 '14 at 20:02
  • Yep, on the Emulator, because CI build agents located somewhere on the other side of the Earth :) So devices are not solution for me – Artem Zinnatullin Jun 01 '14 at 06:45
  • anyone? tried everything, still facing this problem. – C0D3LIC1OU5 Jul 24 '14 at 21:33

3 Answers3

3

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);
}
swanson
  • 7,377
  • 4
  • 32
  • 34
2

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

Quentin Klein
  • 1,263
  • 10
  • 27
0

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

Yuchao Zhou
  • 1,062
  • 14
  • 19