Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check DatePicker calendar value in Android Espresso framework

I've got an activity with two DatePicker controls and some text inputs and buttons. I would like to test some scenario by espresso:

  1. Set date to first DatePicker.
  2. Set some text and click buttons witch trigger my business logic and set date to second DatePicker.
  3. Check date of second DatePicker.

In 3rd 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 of withText to check DatePicker Calendar/Date value? Thanks for any help.

like image 432
maniek099 Avatar asked Jun 29 '17 21:06

maniek099


1 Answers

To achieve what you want you need two things:

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

The easiest way is to to make use of the PickerActions.setDate method:

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

To get this working, you have to add the com.android.support.test.espresso:espresso-contrib library in your gradle file and maybe exclude some libraries, please check my linked answers above if you have trouble with that.

For the second part, to check the date of a DatePicker I think you have to use a custom matcher.

You could 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());
        }
    };
}

Which you then can use to check a date like this:

onView(withId(R.id.end_date_picker)).check(matches(matchesDate(2017, 6, 30)));
like image 50
stamanuel Avatar answered Nov 18 '22 06:11

stamanuel