我正在我的 Activity 中测试一个 fragment 的启动,所以在执行了一个将要启动该 fragment 的按钮后,我测试了在启动的 fragment 中 View 中是否存在一个文本,但是测试失败了,即使该 fragment 在我的手机上启动,甚至在 View 层次结构中也显示该文本存在:
View Hierarchy:
+--------->AppCompatTextView{id=2131886318, res-name=text3_textView, visibility=VISIBLE, width=768, height=68, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=695.0, text=Pour finaliser votre inscription nous avons besion
d'une photo de profil, input-type=0, ime-target=false, has-links=false}
此处测试失败:
onView(withText("photo de profil")).check(matches(isDisplayed()));
我想知道为什么 espresso 没有通过这个测试,是因为它没有等待 fragment 的启动吗?
顺便说一句,我关闭了动画。
最佳答案
espresso withText方法匹配所有字符串都相等。
在您的情况下,您需要匹配字符串是否以您的字符串结尾。
你的代码应该是这样的:
onView(withText(endsWith("photo de profil"))).check(matches(isDisplayed()));
这里有更多示例:http://qathread.blogspot.com.br/2014/01/discovering-espresso-for-android.html
关于Android:Espresso,在层次结构中找不到匹配的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779982/