Android Epresso:DatePicker单击确定添加一年而不是验证

Geo*_*tic 2 android datepicker android-espresso

在Google Espresso测试中,我正在尝试单击DatePickerDialog的"确定"按钮.

但不是验证我的输入,它只是添加一年而不关闭对话框.

似乎点击是在年份专栏的"+"按钮上完成的.这是一个Espresso错误还是我错过了什么?

这是我的Espresso代码(在Kotlin中):

onView(allOf(iz(instanceOf(javaClass<Button>())), withText("OK"),
            isDisplayed()) as Matcher<View>).perform(click())
Run Code Online (Sandbox Code Playgroud)

Sir*_*r0n 6

它可能并不完美(因为我将文本硬编码为"OK"),但我设法在我的测试中使用此代码段执行此操作:

// 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());
Run Code Online (Sandbox Code Playgroud)


smd*_*edy 5

在某些版本的 Android 上,DatePickerDialog 上的“确定”被替换为“设置”或“完成”。尝试:

onView(withId(android.R.id.button1)).perform(click());
Run Code Online (Sandbox Code Playgroud)

这将单击对话框的肯定按钮。不管怎么称呼。


Geo*_*tic 0

将我的测试设备升级到 4.4.4 后,该错误自行消失:这是一个 Android < 4.4 错误 T_T