使用espresso在android中自动编号选择器

问题描述 投票:8回答:4

如何使用浓缩咖啡自动化数字选择器。我想在timePicker中使用espresso设置特定时间。

android android-espresso
4个回答
15
投票

要按类名称匹配View,您只需使用:

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

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

0
投票

对于那些稍后看这个问题的人(像我一样),这可能会有所帮助:DateTimePickerTest使用PickerActions。 PickerActions允许这样的日期选择器代码(Java):

onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month + 1, day));

或者对于时间选择器(Kotlin):

onView(withClassName(Matchers.equalTo(TimePicker::class.java.name))).perform(PickerActions.setTime(0, 10))

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)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.