我正在尝试在Android上编写一个expresso测试,以验证文本视图内容。当我从下面的资源中阅读文本时
@Test
public void changeText_newActivity() {
onView(withId(R.id.mainContent)).check(matches(withText("Hello World!")));
}
以上是使用浓缩咖啡的测试以下是Android活动的代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainContent"
android:text="@string/hello_world"
/>
但是,当我使用硬编码文本替换文本而不是引用资源时,测试失败并获得错误无法在视图层次结构中找到视图
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainContent"
android:text="Hello world!"
/>
下面是我运行测试时的错误
android.support.test.spresso.nomatchingviewException:层次结构中没有视图匹配:带文本:是" Hello World!"
事先感谢您的帮助。
Code "Hello world!"
^
Test "Hello World!"
看到区别吗?W
和w
不一样。
浓缩咖啡正确地找不到这种视图,因为没有数学。
使用以下代码
onView(withId(R.id.mainContent)).check(matches(withText("Hello world!")));
在某些情况下,eqaulignorecase也更好。