如何使用浓缩咖啡自动化数字选择器。我想使用espresso在timePicker中设置特定时间。
答案 0 :(得分:15)
要按类别名称匹配视图,您只需使用:
onView(withClassName(Matchers.equalTo(TimePicker.class.getName())));
获得ViewInteraction对象后,您可以在其上设置一个值,定义并使用ViewAction,如下所示:
public static ViewAction setTime(final int hour, final int minute) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
TimePicker tp = (TimePicker) view;
tp.setCurrentHour(hour);
tp.setCurrentMinute(minute)
}
@Override
public String getDescription() {
return "Set the passed time into the TimePicker";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(TimePicker.class);
}
};
}
答案 1 :(得分:10)
匹配视图,然后执行操作:
ViewInteraction numPicker = onView(withClassName(Matchers.equalTo(NumberPicker.class.getName())));
numPicker.perform(setNumber(1));
创建一个ViewAction来设置数字:
public static ViewAction setNumber(final int num) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
NumberPicker np = (NumberPicker) view;
np.setValue(num);
}
@Override
public String getDescription() {
return "Set the passed number into the NumberPicker";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(NumberPicker.class);
}
};
}
答案 2 :(得分:0)
对于那些以后像我这样看这个问题的人可能会有所帮助:DateTimePickerTest使用了PickerActions。 PickerActions允许为日期选择器提供以下代码(Java):
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month + 1, day));
或者对于时间选择器(科特琳):
onView(withClassName(Matchers.equalTo(TimePicker::class.java.name))).perform(PickerActions.setTime(0, 10))
答案 3 :(得分:0)
接受的答案有问题:它不会在更改事件上触发。因此,(如果需要)您无法测试您的视图是否对更改事件做出反应。
下面的代码(kotlin)无论如何都不是很酷,但我认为这是唯一的方法。
fun setValue(value: Int): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "set the value of a " + NumberPicker::class.java.name
}
override fun getConstraints(): Matcher<View> {
return ViewMatchers.isAssignableFrom(NumberPicker::class.java)
}
// the only way to fire onChange event is to call this private method
override fun perform(uiController: UiController?, view: View?) {
val numberPicker = view as NumberPicker
val setValueMethod = NumberPicker::class.java.getDeclaredMethod(
"setValueInternal",
Int::class.java,
Boolean::class.java
)
setValueMethod.isAccessible = true
setValueMethod.invoke(numberPicker, value, true)
}
}
}
答案 4 :(得分:0)
我找到了解决问题的方法,Luigi Massa Gallerano的解决方案不会触发变更侦听器。
除了他的方法外,您还需要添加一种包装方法,该方法每次都要向上和向下滑动一次。尽管原先的值会丢失,但这会触发changeListener。
fun setNumberPickerValue(viewInteraction: ViewInteraction, value: Int) {
viewInteraction.perform(setValue(value))
viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_CENTER, GeneralLocation.BOTTOM_CENTER, Press.FINGER))
SystemClock.sleep(50)
viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER))
SystemClock.sleep(50)
}
给出的示例使用NumberPicker和Kotlin,但原则上是相同的。
答案 5 :(得分:0)
我使用了内置的TimePickerDialog。
(科特琳)
fun setAlarmTime(){
val calendar = Calendar.getInstance()
onView(isAssignableFrom(TimePicker::class.java)).perform(
PickerActions.setTime(
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE) + 2
)
)
onView(withText("OK")).perform(click())
}
首先,您应该打开TimePickerDialog,然后使用此代码。时间将是当前时间+ 2分钟。设置完成后,单击确定按钮。