2016-02-12 94 views
1

根據我在Espresso作弊表中看到的內容,有兩種方法檢查視圖的可見性,isDisplayed()isCompletelyDisplayed()如何查看Espresso在屏幕外的視圖的可見性?

我在我的主屏幕上有滑動佈局,我有幾個視圖。我用下面的命令檢查其中之一:

onView(withId(R.id.payment_btn)).check(matches(isDisplayed())); 

但是,下面的錯誤測試停止,顯示:

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 

然後我想,既然視圖是不可見的,我可以測試它由以下測試:

onView(withId(R.id.payment_btn)).check(doesNotExist()); 

然而,測試停止並顯示以下信息:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: View is present in the hierarchy: 
Expected: is <false> 
Got: <true> 

因此,任何想法如何檢查視圖的可見性關閉屏幕將不勝感激。謝謝。

回答

7

當視圖在屏幕之外時,它不會顯示,但它仍然存在於視圖層次結構中。要檢查屏幕上未顯示視圖,請使用:onView(withId(R.id.payment_btn)).check(matches(not(isDisplayed())));

如果要檢查是否顯示視圖,則必須滾動/滑動到視圖,以使其變爲可見。

+0

謝謝,我剛剛發現它,並想回答它:)很酷,謝謝你的解釋... – Hesam