Android 斯纳克巴和浓缩咖啡有时会失败

Android 斯纳克巴和浓缩咖啡有时会失败,android,android-espresso,Android,Android Espresso,正如标题所说,它有时失败,有时成功 android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view. Expected: is displayed on the screen to the user Got: "AppCompatTextV

正如标题所说,它有时失败,有时成功

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.

Expected: is displayed on the screen to the user
Got: "AppCompatTextView{id=2131492981, res-name=snackbar_text, visibility=VISIBLE, width=444, height=71, 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=18.0, y=0.0, text=Network Error, input-type=0, ime-target=false, has-links=false}"
堆栈跟踪的第一行显示espresso无法在屏幕上看到Snackbar。但第二行指出,它实际上看到了一个Snackbar,它的
visibility=VISIBLE
text=networkerror
,这是正确的

我很困惑,怎么了

这是我的测试代码:

activityRule.launchActivity(new Intent());
onView(withText("Network Error")).check(matches(isDisplayed()));

PS:当我运行整个测试套装时,它大部分失败;但有时,当我独自运行这个测试时,它也会失败。有些时候它通过绿色,但没有任何图案,似乎是随机的。

太晚了!但我希望这对其他人有帮助:


我也遇到了类似的问题。我通过以下方式解决了这个问题:

  • 如前所述取消动画的伪装

  • 在从服务器获取数据后,我显示了SnackBar,所以我还必须等待数据被获取。我设法用anwser中描述的方法解决了这个问题

  • 然后我成功地检查了SnackBar


    我希望我的观点会有所帮助

    谢谢大家!!我很高兴有这样一个解决方案,它不需要放弃UI测试。实际上,我也尝试了这个方法,我也遇到了同样的问题:(
    private void checkSnackBarDisplayedByMessage(@StringRes int message) {
        onView(withText(message))
            .check(matches(withEffectiveVisibility(
                ViewMatchers.Visibility.VISIBLE
        )));
    }