使用DatePicker记录Espresso测试

Rob*_*Rob 2 android android-espresso

录像机产生的代码在录制后立即失败.

原因是,在录制时,我点击年份,年份微调器弹出,我向后滚动,然后选择其中一年.录像机不捕获滚动.

在Xcode中,他们添加了一种滚动到项目的方法.在Espresso中找不到类似的东西.

(使用Android Studio 2.3.)

sta*_*uel 10

我没有在很长一段时间内使用录音机,而是手工编写我的测试,所以不确定这个答案对你有帮助.

我用这一行在datepicker中设置日期:

onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth));
Run Code Online (Sandbox Code Playgroud)

本PickerActions是一个特殊的咖啡支持库中-中espresso-contrib-像这样把它添加到您的gradle这个文件(我需要一些排除,防止编译错误,由于不匹配支持库版本):

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'
}
Run Code Online (Sandbox Code Playgroud)

然后我在辅助方法中使用它,单击打开日期选择器的按钮,设置日期并通过单击确定按钮确认它:

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

然后在我的测试中使用它:

TestHelper.setDate(R.id.date_button, 2017, 1, 1); 
//TestHelper is my helper class that contains the helper method above
Run Code Online (Sandbox Code Playgroud)