Espresso 測試框架 - 檢視斷言



如前所述,檢視斷言用於斷言實際檢視(使用檢視匹配器查詢)和預期檢視相同。示例程式碼如下:

onView(withId(R.id.my_view)) .check(matches(withText("Hello")))

這裡:

  • onView() 返回對應於匹配檢視的 ViewInteration 物件。ViewInteraction 用於與匹配的檢視進行互動。

  • withId(R.id.my_view) 返回一個檢視匹配器,它將與 id 屬性等於 my_view 的檢視(實際檢視)匹配。

  • withText(“Hello”) 也返回一個檢視匹配器,它將與文字屬性等於 Hello 的檢視(預期檢視)匹配。

  • check 是一個方法,它接受型別為 ViewAssertion 的引數,並使用傳入的 ViewAssertion 物件進行斷言。

  • matches(withText(“Hello”)) 返回一個檢視斷言,它將執行斷言實際檢視(使用 withId 查詢)和預期檢視(使用 withText 查詢)相同這一實際工作

讓我們學習 Espresso 測試框架提供的一些用於斷言檢視物件的方法。

doesNotExist()

返回一個檢視斷言,確保檢視匹配器找不到任何匹配的檢視。

onView(withText("Hello")) .check(doesNotExist());

這裡,測試用例確保沒有文字為 Hello 的檢視。

matches()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並與目標檢視匹配器匹配的檢視匹配。

onView(withId(R.id.textView_hello)) .check(matches(withText("Hello World!")));

這裡,測試用例確保具有 id R.id.textView_hello 的檢視存在並與文字為 Hello World! 的目標檢視匹配。

isBottomAlignedWith()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且底部與目標檢視匹配器對齊。

onView(withId(R.id.view)) .check(isBottomAlignedWith(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且底部與具有 id R.id.target_view 的檢視對齊。

isCompletelyAbove()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且完全位於目標檢視匹配器上方。

onView(withId(R.id.view)) .check(isCompletelyAbove(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且完全位於具有 id R.id.target_view 的檢視上方。

isCompletelyBelow()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且完全位於目標檢視匹配器下方。

onView(withId(R.id.view)) .check(isCompletelyBelow(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且完全位於具有 id R.id.target_view 的檢視下方。

isCompletelyLeftOf()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且完全位於目標檢視匹配器左側。

onView(withId(R.id.view)) .check(isCompletelyLeftOf(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且完全位於具有 id R.id.target_view 的檢視左側。

isCompletelyRightOf()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且完全位於目標檢視匹配器右側。

onView(withId(R.id.view)) .check(isCompletelyRightOf(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且完全位於具有 id R.id.target_view 的檢視右側。

isLeftAlignedWith()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且左側與目標檢視匹配器對齊。

onView(withId(R.id.view)) .check(isLeftAlignedWith(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且左側與具有 id R.id.target_view 的檢視對齊。

isPartiallyAbove()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且部分位於目標檢視匹配器上方。

onView(withId(R.id.view)) .check(isPartiallyAbove(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且部分位於具有 id R.id.target_view 的檢視上方。

isPartiallyBelow()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且部分位於目標檢視匹配器下方。

onView(withId(R.id.view)) .check(isPartiallyBelow(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且部分位於具有 id R.id.target_view 的檢視下方。

isPartiallyLeftOf()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且部分位於目標檢視匹配器左側。

onView(withId(R.id.view)) .check(isPartiallyLeftOf(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且部分位於具有 id R.id.target_view 的檢視左側。

isPartiallyRightOf()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且部分位於目標檢視匹配器右側。

onView(withId(R.id.view)) .check(isPartiallyRightOf(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且部分位於具有 id R.id.target_view 的檢視右側。

isRightAlignedWith()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且右側與目標檢視匹配器對齊。

onView(withId(R.id.view)) .check(isRightAlignedWith(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且右側與具有 id R.id.target_view 的檢視對齊。

isTopAlignedWith()

接受目標檢視匹配器並返回一個檢視斷言,確保檢視匹配器(實際檢視)存在並且頂部與目標檢視匹配器對齊。

onView(withId(R.id.view)) .check(isTopAlignedWith(withId(R.id.target_view)))

這裡,測試用例確保具有 id R.id.view 的檢視存在並且頂部與具有 id R.id.target_view 的檢視對齊。

noEllipsizedText()

返回一個檢視斷言,確保檢視層次結構不包含省略號或被截斷的文字檢視。

onView(withId(R.id.view)) .check(noEllipsizedText());

noMultilineButtons()

返回一個檢視斷言,確保檢視層次結構不包含多行按鈕。

onView(withId(R.id.view)) .check(noMultilineButtons());

noOverlaps()

返回一個檢視斷言,確保可賦值給 TextView 或 ImageView 的後代物件不會相互重疊。它還有另一個選項,它接受目標檢視匹配器並返回一個檢視斷言,確保與目標檢視匹配的後代檢視不會重疊。

廣告
© . All rights reserved.