【问题标题】:How to make a test in Espresso that checks a string that contains HTML tag?如何在 Espresso 中进行测试以检查包含 HTML 标记的字符串?
【发布时间】:2019-10-29 11:48:24
【问题描述】:

我正在对一个 android Fragment 进行一些 UI 测试,我的代码中有以下字符串:

textView.text = Html.fromHtml("$updateText <u>Change</u>?")

在我的测试中,我有这个代码:

recoverPasswordAssert{

    checkTextIsVisible("Number format. Change?")
}

当我运行测试时,会发生此错误:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Number format. Change?"

我认为它缺少片段代码中的 HTML 标签,但是如何在 Espresso 测试中获取标签?

【问题讨论】:

  • 您是否尝试过:“数字格式。更改?”在您的 checkTextIsVisible() 中查看它是否正在寻找它?实际文本的呈现方式可能与底层 String 不同。
  • 感谢您的评论。是的,我尝试了,但我收到了这个错误:androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching:

标签: android html testing automated-tests android-espresso


【解决方案1】:
val text = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.<string_id>)
onView(withText(text)).check(matches(isDisplayed()))

其中&lt;string_id&gt; 来自strings.xml,格式如下:

<string name="some_string_key"><![CDATA[Example HTML text with <u>this</u> and <b>that</b>.]]></string>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多