使用Espresso测试Snackbar

时间:2016-02-04 15:43:31

标签: android-espresso android-snackbar

我使用以下方法对其进行测试:

onView(allOf(withId(android.support.design.R.id.snackbar_text), withText("Network Error")))
        .check(matches(isDisplayed()));

但最不可能is displayed on the screen to the usery坐标为0而失败:... x=18.0, y=0.0, ...

完整日志:

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}"

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您可以使用withEffectiveVisibility而不是isDisplayed,因此它是否实际显示在当前屏幕中并不重要:

private void checkSnackBarDisplayedByMessage(@StringRes int message) {
    onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(message)))
        .check(matches(withEffectiveVisibility(
            ViewMatchers.Visibility.VISIBLE
    )));
}