android - Snackbar 和 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: "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。但第二行指出它实际上看到了一个带有 visibility=VISIBLEtext=Network Error 的 Snackbar,这是正确的。

我很困惑,这是怎么回事?

这是我的测试代码:

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

PS:当我运行整个测试服时,它大多失败了;但有时当我单独运行此测试时它也会失败。有时它会变成绿色,但没有任何图案,似乎是随机的。

最佳答案

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

Testing Snackbar show with Espresso

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

关于android - Snackbar 和 Espresso 有时会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188183/

相关文章:

android - 在生产代码中包含 Toast 的单元测试失败

android - 如何使用 Android Espresso 单击 RecyclerView 中的每个项目

android - Android Text to Speech 说完后如何 toast

android - Android 上的 OpenGL ES GLSL 着色器未加载正确的版本?

android osx 我的机器人没有出现在我的 mac 上

android - 如何使用 Android Espresso 测试 TextInputLayout 值(提示、错误等)?

java - 需要在AsynkTask中等待broadcastReceiver中的事件

带有 gradle 插件 2.0.0-alhpa8 的 Android studio 2.0 Preview 8,内存警告尽管堆大小为 4Gb

android - Espresso 单击 Textview 中的特定子字符串

java - android.util.Pair 在 androidTest 中将 String[] 作为参数,但在(单元)测试中为 null