Assert proper number of items in list with espresso

前端 未结 2 1811
醉话见心
醉话见心 2020-12-19 03:07

What is the best way to inspect and assert that a listview is the expected size with android espresso?

I wrote this matcher, but don\'t quite know how to integrate i

2条回答
  •  执笔经年
    2020-12-19 03:24

    Figured this out.

    class Matchers {
      public static Matcher withListSize (final int size) {
        return new TypeSafeMatcher () {
          @Override public boolean matchesSafely (final View view) {
            return ((ListView) view).getCount () == size;
          }
    
          @Override public void describeTo (final Description description) {
            description.appendText ("ListView should have " + size + " items");
          }
        };
      }
    }
    

    If expecting one item in the list, put this in the actual test script.

    onView (withId (android.R.id.list)).check (ViewAssertions.matches (Matchers.withListSize (1)));
    

提交回复
热议问题