Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert proper number of items in list with espresso

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 it into the test.

public static Matcher<View> withListSize (final int size) {
    return new TypeSafeMatcher<View> () {
        @Override public boolean matchesSafely (final View view) {
            return ((ListView) view).getChildCount () == size;
        }

        @Override public void describeTo (final Description description) {
            description.appendText ("ListView should have " + size + " items");
        }
    };
}
like image 566
Cory Roy Avatar asked May 20 '15 21:05

Cory Roy


People also ask

What is Instrumentation Espresso?

We will use Espresso; a UI test framework that creates automated tests that run on an actual device or emulator. Instrumentation tests are meant to simulate the actions of a user and allow us to test the app through the stages of the android activity lifecycle.

How do you scroll down in espresso?

You can use SWIPE to scroll to the bottom of the screen: Espresso. onView(ViewMatchers. withId(R.


1 Answers

Figured this out.

class Matchers {
  public static Matcher<View> withListSize (final int size) {
    return new TypeSafeMatcher<View> () {
      @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)));
like image 89
Cory Roy Avatar answered Nov 10 '22 10:11

Cory Roy