gpt4 book ai didi

android - 在 Android Espresso 中从日历中选择日期

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:10 26 4
gpt4 key购买 nike

我正在使用 Espresso 测试 Android 应用程序。我有一个带有 androidInputType=dateEditText 小部件。当我用手指触摸这个控件时,会弹出一个日历供我选择日期。

如何在 Espresso 中自动执行此操作?我到处都看了,我想不通。 typeText() 肯定不行。

最佳答案

原来是我在这里回答的,但在另一个问题的范围内:Recording an Espresso test with a DatePicker - 所以我从那里重新发布了我的改编答案:

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

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

这使用 PickerActions,它是 espresso 支持库的一部分 - 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'
}

然后您可以创建一个辅助方法,它单击打开日期选择器的 View ,设置日期并通过单击确定按钮确认它:

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

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

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

关于android - 在 Android Espresso 中从日历中选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43149728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com