Question

I'm trying to implement this using Google's Espresso, however I'm not finding any ViewAssertion or ViewAction that'd allow me to do this.

I'm not sure if these can be done using bundled in matchers or should I write my own.

Thanks!

Was it helpful?

Solution

Even though @denys answer seemed to be correct at first glance, it looks like it works only on certain cases (as suggested by Espresso author). The proper way to scroll to an specific item is as replied here on android-test-kit-discuss forum, and reproduced below:

onData(instanceOf(Item.class))
            .inAdapterView(allOf(withId(android.R.id.list), isDisplayed()))
            .atPosition(9)
            .check(matches(isDisplayed()));

OTHER TIPS

It could look something like this:

1. Find the number of elements in listView and save it in some variable:

    final int[] counts = new int[1]; 
    onView(withId(R.id.some_list_view)).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) {

        }
    }));

2. Then, knowing the number of elements in listView you can scroll to any element within the range.

onData(anything()).inAdapterView(R.id.some_list_view).getPosition(<item_index>).perform(scrollTo())

If you already have this specific number then you have do something like this:

onData(is(instanceOf(yourClass.class)))
    .inAdapterView(withId(R.id.some_list_view))
    .atPosition(spicificNumber)
    .perform(scrollTo());

This will fail if ListView doesn't have an item at position with specificNumber.

EDITED:

Take a look also at this example - Espresso samples. It will help you to check if there are no more items in the list after spesificNumber.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top