'Recording an Espresso test with a DatePicker

The test recorder produces code that promptly fails on being run after recording.

The reason is that while recording, I tap the year, the year spinner pops up, I scroll back and then select one of the years. The recorder does not capture the scrolling.

In Xcode, they added a method to scroll to the item. I could not find something akin in Espresso.

(Using Android Studio 2.3.)



Solution 1:[1]

Note: This answer is intended to supplement @stamanuel's answer.

To see an example in the Android source code of an Android instrumentation test which opens a DatePickerDialog and selects a date, refer to the PickerActionsTest.java file.

The two key lines of code in that file are the following:

// Sets a date on the date picker widget
onView(isAssignableFrom(DatePicker.class)).perform(setDate(1980, 10, 30));

// Confirm the selected date. This example uses a standard DatePickerDialog
// which uses
// android.R.id.button1 for the positive button id.
onView(withId(android.R.id.button1)).perform(click());

As @stamanuel has mentioned in his answer, the PickerActions.setDate(year:monthOfYear:dayOfMonth:) method is defined in the androidx.test.espresso:espresso-contrib library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1