Snackbar和Espresso有时会失败

7
正如标题所述,有时它会失败,而有时它会成功。
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=VISIBLEtext=Network Error,这是正确的。

我很困惑,发生了什么?

这是我的测试代码:

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

PS:当我运行整个测试套件时,它通常会失败;但有时候仅运行这个测试也会失败。其他一些时候,测试通过了,但没有任何模式,似乎是随机的。

2个回答

5
晚了!但我希望对其他人有所帮助:

使用Espresso测试Snackbar显示

private void checkSnackBarDisplayedByMessage(@StringRes int message) {
    onView(withText(message))
        .check(matches(withEffectiveVisibility(
            ViewMatchers.Visibility.VISIBLE
    )));
}

谢谢!我很高兴有一个解决方案,不需要放弃UI测试。 - AdamMc331
1
实际上我刚刚尝试了一下,结果也遇到了相同的问题。:( - AdamMc331

1

我遇到了类似的问题。我通过以下方法解决:

  1. 按照此处所述禁用动画。

  2. 在从服务器获取数据后,我会显示SnackBar,因此我还必须等待数据被获取。我按照这个答案中描述的使用IdlingResource来解决它。

然后我成功地检查了SnackBar。

希望我的观点能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接