Thursday, 30 July 2015

android espresso, assert list length -- ListView and Spinner


Wrong solution:

I found this online, which is working sometime, but not all the time:

onData(instanceOf(ProductVO.class))
        .inAdapterView(withId(R.id.batch_picking_detail_product_list))
        .atPosition(i - 1)
        .check(matches(isDisplayed()));



My Solution:


onView(withId(R.id.batch_picking_detail_product_list)).check(matches(ListMatcher.withListSize(i)));




class ListMatcher {
    public static Matcher<View> withListSize (final int size) {
        return new TypeSafeMatcher<View>() {
            int length;
            @Override public boolean matchesSafely (final View view) {
                length = ((AdapterView) view).getAdapter().getCount();

                return length == size;
            }

            @Override public void describeTo (final Description description) {
                description.appendText ("ListView should have " + size + " items, the actual size is " + length);
            }
        };
    }
}


No comments:

Post a comment