[Robotium]如何滚动并点击GridVIew中的项目?

时间:2016-10-05 21:24:14

标签: android android-testing robotium robotium-recorder

我正在为我的应用程序开发一个测试用例,有标签,每个标签中的每个标签都有个。{/ p>

现在我正在尝试在第一个选项卡中滚动gridView并选择checkBox然后单击第二个选项卡并滚动并选择checkBox这里是我的代码

 GridView gridView = (GridView) solo.getView("truck_points");
    int gvCount = gridView.getCount();
    int nColumn = gridView.getNumColumns();
    int currentChilds = gridView.getChildCount();
    int numOfSelectedItems = randomItem(gvCount);
    int r = 14;
    int index = r%nColumn;
    scrollListTo(gridView, r, getInstrumentation());
    ViewGroup viewGroup = (ViewGroup) gridView.getChildAt(index);
    CheckBox checkBox = (CheckBox) viewGroup.getChildAt(0);
    solo.clickOnView(viewGroup);
    Log.e(TAG,checkBox.getText() + " item: "+ r+", index: "+index+", column: "+ nColumn);

    //Click on Post-Trip
    solo.clickOnView(solo.getView(android.widget.TextView.class, 9));
    solo.sleep(1000);
    solo.clickOnView(solo.getView("start",1));

    GridView gridView1 = (GridView) solo.getView("truck_points");
    int gvCount1 = gridView1.getCount();
    int nColumn1 = gridView1.getNumColumns();
    int currentChilds1 = gridView1.getChildCount();
    int numOfSelectedItems1 = randomItem(gvCount1);
    int r1 = 17;
    int index1 = r1%nColumn1;
    scrollListTo(gridView1, r1,getInstrumentation());
    ViewGroup viewGroup1 = (ViewGroup) gridView1.getChildAt(index1);
    CheckBox checkBox1 = (CheckBox) viewGroup1.getChildAt(0);
    solo.clickOnView(viewGroup1);
    Log.e(TAG,checkBox1.getText() + " item: "+ r1+", index: "+index1+", column: "+ nColumn1);
    solo.sleep(3000);

scrollListTo方法:

public <T extends AbsListView> void scrollListTo(final T listView,
                                                 final int index, Instrumentation instrumentation) {
    instrumentation.runOnMainSync(new Runnable() {
        @Override
        public void run() {
            listView.setSelection(index);
        }
    });
    instrumentation.waitForIdleSync();
}

现在在第一个选项卡中滚动并选择非常好的日志输出:

10-05 22:41:50.786 1486-1517/? E/CheckTAG: Fifth Wheel item: 14, index: 2, column: 4

那是对的。
在选项卡二中,它不会滚动或选择,而是在日志输出中查看正确的数据

10-05 22:41:56.106 1486-1517/? E/CheckTAG: Front Axle item: 17, index: 1, column: 4

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是当我替换这个

时它起作用了
GridView gridView1 = (GridView) solo.getView("truck_points");

通过

GridView gridView1 = (GridView) solo.getView("truck_points",1);