【发布时间】:2021-07-05 01:32:11
【问题描述】:
我想为 Espresso 创建一个自定义正则表达式匹配器,以便我可以检查屏幕上的文本是否包含格式为 HH:mm 的时间,例如 23:34 或 04:23
我有一个正则表达式匹配器类:
class RegexMatcher(private val regex: String) :
BoundedMatcher<View, TextView>(TextView::class.java) {
private val pattern = Pattern.compile(regex)
override fun describeTo(description: Description?) {
description?.appendText("Checking the matcher on received view: with pattern=$regex")
}
override fun matchesSafely(item: TextView?) =
item?.text?.let {
pattern.matcher(it).matches()
} ?: false
}
还有一个功能:
private fun withPattern(regex: String): Matcher<in View>? = RegexMatcher(regex)
屏幕上的文字说:Sometext 08:23时间当然是动态的
我的 UI 检查是这样写的:
onView(withText(startsWith("Sometext"))).check(matches(withPattern("/(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/")))
但测试总是失败,我不知道为什么。即使我只是使用像/^Sometext 这样简单的东西,它也会失败。谁能帮帮我?
【问题讨论】:
标签: android regex kotlin android-espresso uitest