Android:Espresso,层次结构中未找到匹配的视图

Android: Espresso, No views in hierarchy found matching

我正在测试 activity 中片段的启动,因此在单击要启动片段的按钮后,我测试了已启动片段中是否存在可见文本,但是即使在我的 phone 上启动了该片段,测试也失败了,甚至在视图层次结构中也显示文本存在:

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 没有通过这个测试,是因为它没有等待片段的启动吗?

顺便说一句,我关闭了动画。

espresso withText方法匹配所有字符串都相等

在你的情况下,如果字符串以你的字符串结尾,你需要匹配。

你的代码应该是这样的:

onView(withText(endsWith("photo de profil"))).check(matches(isDisplayed()));

这里有更多例子:http://qathread.blogspot.com.br/2014/01/discovering-espresso-for-android.html

我在做什么:

    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(SplashScreenActivity::class.java)

由于这条规则,Espresso 库 正在 SplashScreenActivity 上搜索登录 EditText 视图。可悲的是,此登录 EditText 视图实际上存在于 LoginActivity 中,结果测试用例因上述错误而失败。

我做了什么让一切正常:

    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(LoginActivity::class.java)

一旦我将上述规则中的 ActivitySplashScreenActivity 更改为 LoginActivityEspresso 轻松找到此登录 EditText 视图并且所有测试均已通过。

只需确保将 Espresso 指向正确的 Activity/Fragment,其中 View 实际存在。