使用DatePicker记录Espresso测试 [英] Recording an Espresso test with a DatePicker

查看:80
本文介绍了使用DatePicker记录Espresso测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记录器生成的代码在记录后立即无法运行.

The 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 and I scroll back, then select one of the years. The recorder does not capture the scrolling.

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

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

(使用Android Studio 2.3.)

(Using Android Studio 2.3.)

推荐答案

我已经很长时间没有使用记录仪了,而是手动编写了我的测试,所以不确定这个答案是否对您有帮助.

I have not used the recorder in a long time and instead wrote my tests by hand, so not sure if this answer is of help to you.

我使用这一行在日期选择器中设置日期:

I use this line to set the date in a datepicker:

onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth));

PickerActions在特殊的espresso支持库中-espresso-contrib-像这样将其添加到gradle文件中(我需要几个排除项,以防止由于支持库版本不匹配而导致编译错误):

The PickerActions is within a special espresso support library - the espresso-contrib - add it like this to your gradle file (I need several excludes to prevent compile errors due to mismatching support library version):

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'
}

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

I then use this in a helper method which clicks the button that opens the datepicker, sets the date and confirms it by clicking the ok button:

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());
}

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

and then use it like this in my tests:

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

这篇关于使用DatePicker记录Espresso测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆