在 Test Android App by Use Espresso (一) 中,提到 Espresso API 的操作的流程 Find View、Perform Action和Check。觀察程式後,可以發現不管是Perform a Action還是Check,都需要先找到View。
1. Find View by Other Attribute
plain text
onView(withId(R.id.button)) 這裡演示了onView()最基本的例子,找到一個id為R.id.button的View
plain text
onView(withText("MyButton")) 但是有時候並非所有View都有設定id,此時我們可以使用上面這種方式。透過指定View上的文字來找到對應的View。
這時你可能會有疑問,若我有很多button都有相同的文字呢?
2. Find View by Multiple Matcher
在這種情況,如果還是使用withText(String)來搜尋View的話,測試就會發出AmbiguousViewMatcherException導致測試失敗。
此時我們就需要更多的條件,用來更明確的指出我們需要某一個特定的View。
plain text
onView(allOf(withId(R.id.button), withText("MyButton"))) 透過allOf(Matcher<? super T>… matchers),我們可以指定更多的條件來獲取我們所需要的那個唯一的View。