2016-12-23 24 views
2

以下語句不起作用,因爲doesNotExist()返回ViewAssertion而不是匹配器。任何方式,使其工作沒有try-catch意大利濃咖啡檢查視圖或者不顯示或不顯示

.check(either(matches(doesNotExist())).or(matches(not(isDisplayed())))); 
+0

你不喜歡'try-catch'嗎? –

+1

@SimonSchnell它不適合Hamcrest匹配器的語法。我認爲可能會有更美麗的解決方案。 – ferbeb

+1

從我的角度來看,這種說法是不正確的。 '(not(isDisplayed()))'用於檢查在層次結構中存在的視圖不顯示,但是'doesNotExist()'驗證該視圖根本不存在於層次結構中。他們互相矛盾。 – denys

回答

2

我有同樣的問題,我的觀點一個會不會有一定的看法,但最初可能會增加它後來將其隱藏。用戶界面依賴於其他背景活動的狀態被破壞。

我最終只是寫上doesNotExist執行情況的變化:

public class ViewAssertions { 
    public static ViewAssertion doesNotExistOrGone() { 
     return new ViewAssertion() { 
      @Override 
      public void check(View view, NoMatchingViewException noView) { 
       if (view != null && view.getVisibility() != View.GONE) { 
        assertThat("View is present in the hierarchy and not GONE: " 
           + HumanReadables.describe(view), true, is(false)); 
       } 
      } 
     }; 
    } 
} 
0

如果你想檢查看法不不是在層次結構存在,請使用以下斷言。

ViewInteraction.check(doesNotExist()); 

如果要檢查層次結構中是否存在視圖,但不顯示給用戶,請使用下面的斷言。

ViewInteraction.check(matches(not(isDisplayed()))); 

希望這會有所幫助。

相關問題