【问题标题】:Android Espresso - How to check if string was abbreviated [duplicate]Android Espresso - 如何检查字符串是否缩写[重复]
【发布时间】:2020-06-08 07:01:56
【问题描述】:

我想检查我的操作栏的字符串是否太长并且缩写为“...”。 我正在使用此代码来检查我的操作栏的标题是否正确。如果字符串太长并且被缩写,它不会失败。 我也尝试过使用 .isCompletelyDisplayed 但这也不起作用。

onView(withId(R.id.action_bar).check(matches(withText(text)));

【问题讨论】:

  • if (myString.endsWith("...")) 类似的东西?
  • 我不知道如何获取实际显示的字符串。从action_bar获取字符串总是得到完整的字符串,即使只显示了一部分

标签: java android mobile android-espresso


【解决方案1】:

使用普通的文本视图,您可以编写一个小的Matcher 来检查getEllipsisCount 是否大于0。使用​​工具栏,除非您有一个自定义的TextView 用于平铺,否则会稍微复杂一些。您必须在 Toolbar 视图中查看标题视图(使用 getChildAtgetChildCount)并进行相同的检查。像

val matcher = object : BoundedMatcher<View, Toolbar>(Toolbar::class.java) {
        override fun describeTo(description: Description?) {
        }

        override fun matchesSafely(item: Toolbar?): Boolean {
            for (i in 0 until (item?.childCount ?: 0)) {
                val v = item?.getChildAt(i)
                (v as? TextView)?.let {
                    // check somehow that this textview is the title
                    val lines = it.layout.lineCount
                    if (it.layout.getEllipsisCount(lines - 1) > 0) {
                        return true
                    }
                }
            }
            return false
        }
    }

然后像使用它一样

    onView(withId(R.id.action_bar)).check(matches(matcher))

我自己没有测试过 - 但你明白了。您也可以查看代码LayoutMatchers 以获得一些灵感

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2011-11-27
    • 2020-09-20
    • 2011-11-11
    • 2023-03-31
    • 2018-02-03
    相关资源
    最近更新 更多