Recording Espresso test with DatePicker
The recorder generates code that quits quickly after recording.
The reason is that while recording, I press the year, the spinner turns out a year ago, and I scroll back and then select one of the years. The recorder does not capture scrolling.
In Xcode, they added a method to scroll to an element. Couldn't find something that looks like espresso.
(using Android Studio 2.3.)
source to share
I haven't used the recorder for a long time and instead wrote my tests by hand, so I'm not sure if this answer will help you.
I am using this line to set the date in the datepicker:
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth));
PickerActions
is in the espresso dedicated support library - espresso-contrib
- add it like this to your gradle file (I need a few exceptions to prevent compilation errors due to the support library version mismatch):
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
exclude group: 'com.android.support', module: 'appcompat'
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
exclude module: 'appcompat-v7'
}
Then I use this in a helper method that clicks a button that opens the date, sets the date, and confirms it by clicking ok:
public static void setDate(int datePickerLaunchViewId, int year, int monthOfYear, int dayOfMonth) {
onView(withParent(withId(buttonContainer)), withId(datePickerLaunchViewId)).perform(click());
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth));
onView(withId(android.R.id.button1)).perform(click());
}
and then use it in my tests:
TestHelper.setDate(R.id.date_button, 2017, 1, 1);
//TestHelper is my helper class that contains the helper method above
source to share