2016-02-03 45 views
6

正如標題所說,它有時會失敗,有些則失敗。Snackbar和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這是正確的。

我很困惑,這是怎麼回事?

這是我的測試代碼:

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

PS:當我運行整個測試套裝它主要是失敗;但有時它也會失敗,因爲我只是單獨運行這個測試。有些時候它會變綠,但沒有任何圖案,看起來是隨機的。

回答

4

晚!但我希望這是對別人有幫助的:

Testing Snackbar show with Espresso

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

謝謝!我很高興有這樣的解決方案,不需要放棄UI測試。 – AdamMc331

+0

其實我也試過這個,我也遇到同樣的問題。 :( – AdamMc331

1

我得到類似的問題。我能夠通過以下方式解決它:

  1. 如所述的鬆動動畫here

  2. 我從服務器獲取數據後顯示SnackBar,所以我也必須等到數據被提取。我設法用IdlingResource來解決它,如this anwser所述。

然後我能夠成功檢查SnackBar。

我希望我的觀點能幫上忙。

相關問題