通过列表迭代和点击的Robotium列表中的项目 [英] Iterating through a List and clicking on list items in Robotium

查看:95
本文介绍了通过列表迭代和点击的Robotium列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过列表迭代并单击每个列表元素,开始另一个活动运行在Robotium一些自动化的测试。我有以下的code在我的测试方法:

I'm trying to run some automated tests in Robotium by iterating through a list and clicking on each list element to start another activity. I have the code below in my test method:

code:

      solo.assertCurrentActivity("Wrong activity", MainActivity.class);
      //Clicks on the action bar tab
      solo.clickOnText("Charts"); 


      ArrayList<ListView> list = solo.getCurrentListViews();

      for(int i = 0; i < list.size(); i++) {


         //Clicks on the list item assert that the new activity is started
         solo.clickInList(chartPosition);
         solo.assertCurrentActivity("Json Class", JsonActivity.class);
         //Go back to the list  
         solo.goBack();         


     }

在code以上不点击任何列表项和JUnit测试结果显示,所有测试都通过了,这是非常令人困惑。

The code above does not click on any list items and the JUnit Test results show that all tests are passed which is very confusing.

有谁如何通过Robotium列表成功迭代?

Does anyone how to successfully iterate through a list in Robotium?

我见过类似这样的一个问题,但答案建议看JMock的,而不是不帮。

I've seen another question similar to this but the answer suggests looking at jMock instead which doesn't help.

推荐答案

我有previously使用这些辅助功能略有不同的状态,你需要与列表视图处理大​​多数的:

I have previously used these helper functions in a slightly different state to handle most of what you need with listviews:

public View getViewAtIndex(final ListView listElement, final int indexInList, Instrumentation instrumentation) {
    ListView parent = listElement;
    if (parent != null) {
        if (indexInList <= parent.getAdapter().getCount()) {
            scrollListTo(parent, indexInList, instrumentation);
            int indexToUse = indexInList - parent.getFirstVisiblePosition();
            return parent.getChildAt(indexToUse);
        }
    }
    return null;
}

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

使用这些功能存储在某个地方(可以是静态的,如果你喜欢...我preFER不这样做,但很方便)

With these functions stored somewhere (they can be static if you like... i prefer to not do that but it is convenient)

ListView list = solo.getCurrentListViews().get(0);
for(int i=0; i < list.getAdapter().getCount(); i++){
    solo.clickOnView(getViewAtIndex(list, i, getInstrumentation()))
    solo.assertCurrentActivity("Json Class", JsonActivity.class);
    solo.goBack();
}

您当前的解决方案实际上是在试图遍历所有你必须​​在屏幕上,而不是在ListView的元素列表视图。

Your current solution is in fact trying to iterate through all the listviews you have on screen and not the elements in the listView.

这篇关于通过列表迭代和点击的Robotium列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆