Check DatePicker Calendar Value in Android Espresso System

I have an activity with two controls DatePicker

and some text inputs and buttons. I would like to test some espresso scenario:

  • Set the date for the first DatePicker

    .
  • Set some text buttons and buttons running my business logic and set the date to the second DatePicker

    .
  • Check the date of the second DatePicker

    .

In the third step, I would like to do something like onView(withId(R.id.end_date_picker)).check(matches(withText("25-01-2017")));

. What can I use instead withText

to check the DatePicker Calendar / Date value? Thanks for any help.

+3


source to share


1 answer


To achieve what you want, you need two things:

To set a date for a datepicker, the best way is to use the espresso PickerActions, I wrote about them here and here , but copy some parts of my answer from there:

The easiest way is to use the method PickerActions.setDate

:

onView(withId(R.id.start_date_picker)).perform(PickerActions.setDate(2017, 6, 30));

      

To get this working you need to add the library com.android.support.test.espresso:espresso-contrib

to your gradle file and maybe exclude some libraries, please check my linked answers above if you have problems with this.



In the second part, in order to check the date of the DatePicker, I think you need to use a custom connector.

You can add this method to your code:

public static Matcher<View> matchesDate(final int year, final int month, final int day) {
    return new BoundedMatcher<View, DatePicker>(DatePicker.class) {

        @Override
        public void describeTo(Description description) {
            description.appendText("matches date:");
        }

        @Override
        protected boolean matchesSafely(DatePicker item) {
            return (year == item.getYear() && month == item.getMonth() && day == item.getDayOfMonth());
        }
    };
}

      

What can you use to validate the date, for example:

onView(withId(R.id.end_date_picker)).check(matches(matchesDate(2017, 6, 30)));

      

+3


source







All Articles