In an Google Espresso test, I'm trying to click on the OK button of a DatePickerDialog.
But instead of validating my input, it just add a year and does not close the dialog.
It seems that the click is done on the "+" button of year's column. Is that an Espresso bug or am I missing something?
Here is my Espresso code (in Kotlin):
onView(allOf(iz(instanceOf(javaClass<Button>())), withText("OK"),
isDisplayed()) as Matcher<View>).perform(click())
It's probably not perfect (because I hardcode the text "OK"), but I manage to do it in my tests using this snippet:
// Change the date of the DatePicker. Don't use "withId" as at runtime Android shares the DatePicker id between several sub-elements
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(1989, 8, 25));
// Click on the "OK" button to confirm and close the dialog
onView(withText("OK")).perform(click());
On some versions of Android "OK" on DatePickerDialog is replaced with "Set" or "Done". Try:
onView(withId(android.R.id.button1)).perform(click());
This will click on dialog's positive button. No matter how it is called.
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