English 中文(简体)
• 如何在Espresso测试中就材料制造商采取行动?
原标题:How to perform an action on MaterialDatePicker in an Espresso test?

我有<条码>MaterialDatePicker dialog,我想写一个选择日期的Espresso测试。 不幸的是,我可以为此使用<代码>PickerActions。 • 寻找与此类似的东西:

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

是否有人对此有何想法?

提前感谢!

问题回答

我发现载于上文相关问题,你可能认为,确定“InputMode”为INPUT_MODE_TEXT的日期是有益的。

public static void setDate(LocalDate date){
    onView(withTagValue((Matchers.is((Object) ("TOGGLE_BUTTON_TAG"))))).perform(click());
    onView(withId(com.google.android.material.R.id.mtrl_picker_text_input_date)).perform(replaceText(date.format(DateTimeFormatter.ofPattern("M/d/yy"))));
}

public static void setTime(LocalTime time){
    onView(withId(com.google.android.material.R.id.material_timepicker_mode_button)).perform(click());
    onView(withId(com.google.android.material.R.id.material_hour_text_input)).perform(click());
    onView(allOf(isDisplayed(), withClassName(is(AppCompatEditText.class.getName())))).perform(replaceText(time.format(DateTimeFormatter.ofPattern("hh"))));
    onView(withId(com.google.android.material.R.id.material_minute_text_input)).perform(click());
    onView(allOf(isDisplayed(), withClassName(is(AppCompatEditText.class.getName())))).perform(replaceText(time.format(DateTimeFormatter.ofPattern("mm"))));
}

从非常严格地依靠女方来不改变,这无疑是jan笑的,但它是有效的。

如果你想选择“InputMode”为INPUT_MODE_CALENDAR的日期,那么这有利于我:

onView(
   // the date you are selecting must be visible for this to work 
   withContentDescription("Mon, Jan 1, 1990")
).inRoot(RootMatchers.isDialog())
  .perform(click())

onView(withId(com.google.android.material.R.id.confirm_button))
  .perform(click())

你可以进一步扩大这一答案,选择一个不同的月/日/年,在材料日期Picker布局中点击适当的县。 可在上向女婴ids。

我只想加上“Chantell Osejo”的评论。 某些日期的内容将有所不同:

"Mon, Jan 1, 2024"
"Fri, Dec 29"
"Today Thu, Dec 28"

如果您在选择起算日期时重新使用<条码>MaterialDatePicker.dateRangePicker(>,则选定起算日期的内容说明将如<条码>,Startdate Mon, Jan 1, 2024”

这里是根据@Chantell Osejo的评论编写的“Matier I ve”。 还需要一些改进,感到可以自由发表意见。

fun withDate(date: Date): Matcher<View> {
    fun areDatesEqual(calendar1: Calendar, calendar2: Calendar): Boolean {
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
                calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
                calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
    }

    fun areYearsEqual(calendar1: Calendar, calendar2: Calendar): Boolean {
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
    }
    val now = Calendar.getInstance()
    val selectedDate = Calendar.getInstance().apply {
        time = date
    }

    val isToday = areDatesEqual(now, selectedDate)
    val areYearsEqual = areYearsEqual(now, selectedDate)

    val dateFormat = if (areYearsEqual) {
        SimpleDateFormat("EEE, MMM d")
    } else {
        SimpleDateFormat("EEE, MMM d, yyyy")
    }

    val finalDate = if (isToday) {
        "Today ${dateFormat.format(date)}"
    } else {
        dateFormat.format(date)
    }

    return withContentDescription(finalDate)
}

使用:

val tomorrowCalendar = Calendar.getInstance().apply {
    add(Calendar.DAY_OF_MONTH, 1)
}
onView(withDate(tomorrowCalendar.time))
    .inRoot(RootMatchers.isDialog())
    .perform(click())
onView(withId(com.google.android.material.R.id.confirm_button))
    .perform(click())




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签