I've got an activity with two DatePicker
controls and some text inputs and buttons. I would like to test some scenario by espresso:
DatePicker
.DatePicker
.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.
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With