Espresso,滚动到动态加载ListView的底部



通常在Testcase中向下滚动,我可以使用下面的代码:

 onView( withId( R.id.button)).perform( scrollTo(), click());

然而,在这个测试中,我想向下滚动一个Listview,其中的子元素是动态加载的(我不知道有多少),当视图向下滚动。在浓缩咖啡中有像Robotium一样向下滚动的东西吗?

我workarround

:

int [] childCount =  getListElements();
    for(int i = 0;i<childCount[0];i++) {
        onData(anything()).inAdapterView(withId(R.id.ScheduleOrderListViewListView)).atPosition(i).perform(click());
        childCount =  getListElements();
    }

public int[] getListElements(){
    final int[] counts = new int[1];
    onView(withId(R.id.ScheduleOrderListViewListView)).check(matches(new TypeSafeMatcher<View>() {
        @Override
        public boolean matchesSafely(View view) {
            ListView listView = (ListView) view;
            counts[0] = listView.getCount();
            return true;
        }
        @Override
        public void describeTo(Description description) {
        }
    }));
    return counts;

您应该使用onData()与ListView元素进行交互。如果您想滚动到底部,只需滚动到最后一项。例如:

onData(instanceOf(ListData.class))
        .atPosition(INITIAL_LIST_SIZE - 1)
        .perform(scrollTo());

最新更新