android - 有没有办法让 espresso ViewAssert 用于正则表达式?

标签 android android-espresso ui-automation

我想知道是否有任何方法可以为正则表达式生成espresso ViewAssert,例如:

onView(withId(R.id.element_id)).check(matches(withRegEx("\\+d")));

最佳答案

我试图寻找 Espresso 中已经存在的匹配器,但也没有找到。一个建议是创建您自己的。下面是一个使用 kotlin 的示例:

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
}

这定义了一个匹配器,它将检查 TextView 的文本是否与特定的正则表达式模式匹配。

你可以拥有这个小工厂功能:

 private fun withPattern(regex: String): Matcher<in View>? = RegexMatcher(regex)

然后你可以像这样使用它:

onView(withId(R.id.element_id)).check(matches(withPattern("\\+d")))

希望这对您有所帮助

关于android - 有没有办法让 espresso ViewAssert 用于正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56329325/

相关文章:

android - 在 RecyclerView 中使用 Urls 显示包含图像的 ParseFile

android - AppCompat v22 : Set default textcolor for TextView

gradle - 为其他模块生成覆盖率

安卓 Espresso : Sliding Tabs with RecyclerViews in every tab

android - Android : Google GCM vs. Amazon SNS 上的推送通知?

java - 意式 Espresso 。 NoSuchMethodError UsageTracker

ios - UICollectionView 查询

selenium - 无法通过 Selenium 和 C# 使用 Marionette 驱动程序实例化 Firefox。操作系统错误

javascript - TypeScript 与 UIAutomation(Apple 的库)

android - 选项卡式 Activity ( fragment )内的 WebView