如何在Robotium中编写测试用例Drawer布局

时间:2014-07-02 06:38:01

标签: android testing android-listview robotium drawerlayout

我在Activity中使用DrawerLayout,我必须在Robotium中编写测试用例: 1)如何检查抽屉是打开还是关闭我试过但下面没有工作是我在Robotium中编写的代码:

    public void testOpenAndCloseNavigationDrawer() {
          /* DrawerLayout drawerlayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);*/

           DrawerLayout drawer = ((BalanceActivity)activity).mDrawerLayout;
           assertFalse(drawer.isActionbarDrawerOpened());
           solo.clickOnActionBarHomeButton();
           assertTrue(drawer.isActionbarDrawerOpened());
           solo.clickOnActionBarHomeButton();
           assertFalse(drawer.isActionbarDrawerOpened());
    }

2)我在Activity 1中有两个listview是主listview另一个是drawerlayout中的listview,我能够测试下面的主listview项目是我在Robotium中为主列表视图编写的测试代码:

    public void testNavigationToSubScreens() throws Exception {
            this.solo.waitForActivity(DetailActivity.class);  
            assertEquals(DetailActivity.class, solo.getCurrentActivity().getClass());  
            solo.clickInList(1);
            solo.goBack();
            solo.clickInList(2);
            solo.goBack();
            solo.clickInList(3);
            solo.goBack();
            solo.clickInList(4);
            solo.goBack();
    }

3)如果我们不知道listView

中有多少项,如何测试listView项

1 个答案:

答案 0 :(得分:0)

这对我有用。

public void testOpenAndCloseNavigationDrawer() {
    DrawerLayout drawerlayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    final com.quisk.quiskapp.utils.DrawerLayout drawer = ((BalanceActivity)activity).mDrawerLayout;
    assertFalse(((BalanceActivity)activity).mDrawerLayout.isActionbarDrawerOpened());
    solo.clickOnActionBarHomeButton();
    solo.waitForView(DrawerLayout.class);
    assertTrue(solo.waitForText("NOTIFICATIONS"));
    ListView listView = (ListView) solo.getView(R.id.left_drawer);
    View listElement = listView.getChildAt(1);
    solo.clickOnView(listElement); 
}