Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Epresso: DatePicker Click on OK add a year instead of validate

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())
like image 728
Geob-o-matic Avatar asked Jun 02 '15 13:06

Geob-o-matic


2 Answers

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());
like image 151
Sir4ur0n Avatar answered Sep 20 '22 16:09

Sir4ur0n


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.

like image 20
smdremedy Avatar answered Sep 18 '22 16:09

smdremedy