Espresso
Espresso提供大量的方法用來進(jìn)行UI測試枫绅,這些API可以讓你寫的簡潔和可靠的自動化UI測試亩码,站在用戶的角度測試,所有邏輯是黑盒
Espresso的三個重要功能:
- 元素定位
- 操作元素
- 驗證結(jié)果
使用流程:
onView(ViewMatcher) // 定位元素
.perform(ViewAction) // 操作元素
.check(ViewAssertion); // 驗證結(jié)果
<br />
補充:
-
Espresso 常用方法:
closeSoftKeyboard() onData(Matcher<? extends Object> dataMatcher) onView(Matcher<View> viewMatcher) pressBack() registerIdlingResources(IdlingResource... resources)
Espresso API : <a >https://developer.android.com/reference/android/support/test/espresso/Espresso.html</a>
-
ViewInteraction 常用方法:
check(ViewAssertion viewAssert) // 檢測當(dāng)前view是否匹配ViewAssertion perform(ViewAction... viewActions) // 讓當(dāng)前view執(zhí)行方法參數(shù)中的ViewAction withFailureHandler(FailureHandler failureHandler)
ViewInteraction API : <a >https://developer.android.com/reference/android/support/test/espresso/ViewInteraction.html</a>
--
<br />
1. 元素定位 onView(ViewMatchers) --> ViewInteraction
public static ViewInteraction onView(final Matcher<View> viewMatcher) {}
Espresso.onView
方法接收一個Matcher<View>
類型的入?yún)⒊芫⒎祷匾粋€ViewInteraction
對象疚俱。
ViewMatchers
對象提供了大量的withXXX
方法用來定位元素,常用的有:
withId(int id) // 根據(jù)元素Id屬性
withText(String text) // 根據(jù)元素的text屬性
withText(int resourceId) // 根據(jù)元素的文本id
withHint(int resourceId) // 根據(jù)元素的預(yù)置內(nèi)容屬性
withHint(String hintText) // 根據(jù)元素的預(yù)置內(nèi)容屬性 withResourceName(String name) // 根據(jù)資源文件名
withResourceName(Matcher<String> stringMatcher) // 根據(jù)資源文件id號
withChildwithParent(Matcher<View> parentMatcher)
withClassName(Matcher<String> classNameMatcher)
withContDescriptiion(int resourceId) // 根據(jù)元素的描述(description )屬性
withContDescriptiion(String text) // 根據(jù)元素的描述(description )屬性
以及一系列的isXXX
方法用來判斷元素的狀態(tài), 常用的有:
isDisplayed() //元素處于可見狀態(tài)
isChecked() //元素(checkbox)處于選中狀態(tài)
isNotChecked() //元素(checkbox)處于未被選擇狀態(tài)
isEnabled()
isFocusable() //元素是否處于聚焦屬性
isSelected() //元素處于被選擇狀態(tài)
其他常用
hasFocus() //獲取擁有焦距的元素
如果單一的匹配條件無法精確地匹配出來唯一的控件博敬,我們可能還需要額外的匹配條件友浸,此時可以用AllOf#allOf()
方法來進(jìn)行復(fù)合匹配條件的構(gòu)造, 用法如下:
onView(allOf(withId(id), withText(text)))
ViewMatchers API :<a >https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers.html</a>
2. 操作元素 perform(ViewAction...) --> ViewInteraction
public ViewInteraction perform(final ViewAction... viewActions) {}
當(dāng)定位到元素后,返回一個ViewInteraction
對象偏窝,其perform
方法可以接收一系列ViewAction
用來進(jìn)行模擬用戶操作收恢,
ViewActions
類提供了大量的操作實現(xiàn),常用的有:
clearText() // 清楚文本
typeText(String str) // 輸入文本 str
typeTextIntoFocusedView(String str) // 在聚焦的元素中輸入文本str
closeSoftKeyboard() // 關(guān)閉軟鍵盤
click() // 點擊
doubleClick() // 雙擊
longClick() // 長按
pressBack() // 點擊物理鍵返回
pressKey(int keyCode) // 點擊指定的某個按鍵
pressKey(EspressoKey key) // 點擊指定的某個按鍵
swipeLeft() // 向左輕掃
swipeRight() // 向右輕掃
swipeUp() // 向上輕掃
swipeDown() // 向下輕掃
scrollTo() // 滑動到當(dāng)前元素
ViewActions API :<a >https://developer.android.com/reference/android/support/test/espresso/action/ViewActions.html</a>
3. 驗證結(jié)果 check(ViewAssertion) -> ViewInteraction
public ViewInteraction check(final ViewAssertion viewAssert) {}
最后為了驗證操作是否符合預(yù)期囚枪,我們還是需要定位到元素,獲得一個ViewInteraction
對象劳淆,其check
方法接收了一個ViewAssertion
的入?yún)⒘凑樱撊雲(yún)⒌淖饔镁褪菣z查結(jié)果是否符合我們的預(yù)期。
ViewAssertion
中常見方法
doesNotExist() // 元素不存在
matches(Matcher<? super View> viewMatcher) // 根據(jù)Matcher得到想要的元素
selectedDescendantsMatch(Matcher<View> selector, Matcher<View> matcher) //
ViewAssertion API :<a >https://developer.android.com/reference/android/support/test/espresso/assertion/ViewAssertions.html</a>