2016-02-04 154 views
0

我用下面正在測試它:測試小吃吧與咖啡

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

但它is displayed on the screen to the user最有可能失敗,因爲y座標爲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}" 

任何想法?

+0

請加滿logcat的。你只放了一部分 – piotrek1543

+1

這是最重要的部分,思想。問題是小吃棒動畫從Y = 0開始,因此,有時espresso會立即執行'isDisplayed'檢查,並且由於Y = 0,它將返回false,即使小吃棒存在並且存在於正確的根。我們放棄了這一點,並轉向期望測試。 –

回答

1

可以使用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 
    ))); 
}