編輯:關於 Android 程式設計
Espresso 是 Google 官方推出的 Android 界面測試框架,它能夠讓我們以接近「用戶真實操作」的方式去驅動和驗證 App 的 UI。本文整理自官方文檔的核心概念,並附上實作步驟與範例,方便快速上手。
理解 Espresso,先要記住以下四個關鍵角色:
onView() 與 onData() 與視圖互動,其 API 不依賴任何具體的 View。Matcher<View> 的集合對象,用來定位當前的 View。ViewInteraction.perform() 觸發。ViewInteraction.check() 執行。
一行典型的測試語句如下:
onView(withId(R.id.main_view)).perform(click()).check(matches(isDisplayed()));
其中 withId() 透過 id 取得 ViewMatcher,click() 是 ViewAction,matches() 則屬於 ViewAssertion。
1. 普通 View 用 onView() 加載
// R.id.xx 是唯一 id 時 onView(withId(R.id.main_view));
若目標 View 的 id 並不唯一,直接這樣寫會拋出 AmbiguousViewMatcherException。此時的解法是縮小範圍,用多個屬性共同定位。例如兩個 EditText 的 id 相同、文字不同,想取得內容為 "Hello!" 的那一個:
onView(allOf(withId(R.id.my_view), withText("Hello!")));
2. AdapterView 類型的 item 用 onData() 加載
像是 GridView、ListView、Spinner 這類控件的子項,應改用 onData() 加載。若遇到 R.id 不存在或不唯一的情況,也可以自定義 ViewMatcher 來精準定位。
在匹配到目標 View 後,透過 perform() 執行操作:
// 單一點擊
onView(withId(R.id.main_view)).perform(click());
// 一次執行多個操作
onView(...).perform(typeText("Hello"), click());
// 在 ScrollView 中先捲動再點擊
onView(...).perform(scrollTo(), click());
注意操作會依序執行,例如 click() 會在 scrollTo() 之後才觸發。
透過 check() 執行斷言,通常搭配 matches() 使用:
// 檢查 View 是否包含 "Hello" 內容
onView(...).check(matches(withText("Hello!")));
需要注意的是,若 View 是否顯示尚無法確定,不宜把 isDisplayed() 這類斷言直接放進定位環節,否則容易誤判。應先確認 View 已顯示,再進行內容斷言。
在 Eclipse 中主要是加入對應的靜態 jar 包。官方在 GitHub 上同時提供了 Android Studio 與 Eclipse 的範例專案,使用 Eclipse 時選擇 BasicSampleBundled 即可。
第一步:加入 Android Support Repository(已加入者可略過)。

第二步:在 build.gradle 中加入依賴
// Android JUnit Runner androidTestCompile 'com.android.support.test:runner:0.5' // JUnit4 Rules androidTestCompile 'com.android.support.test:rules:0.5' // Espresso 核心 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' // 測試 DatePicker、RecyclerView、Drawer 等操作所需 androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' // 測試 WebView 所需 androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2' // 測試非同步線程所需 androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'
別忘了完成後點擊 Sync Now。
第三步:建立測試目錄,切換到 Project 視圖,在 src 下建立 androidTest/java,並在該路徑下建立與專案同名的包。

第四步:建立測試類,依 Espresso 的撰寫方式編寫測試碼。
第五步:建立 Test Configuration,依序選擇 Edit Configurations → + → Android Tests,選擇 module 並帶入 AndroidJUnitRunner。

配置好 jar 後,常會遇到 support-annotations 版本衝突:
Error: Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.3.0) and test app (23.1.1) differ.
解法是讓測試與主程式採用相同版本的 support-annotations:
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
// 加入注解,包含 test
compile 'com.android.support:support-annotations:23.3.0'
// 解決衝突
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
以下範例展示如何測試 Button 點擊與 ListView 子項點擊。測試類別中別忘了標註 @RunWith、@Rule、@Test 等注解:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void clickButtonTest() {
onView(withId(R.id.main_change)).perform(click());
}
@Test
public void testAdapterView() {
// 點擊 listView 中內容為 "2" 的 item
onData(Matchers.allOf(
Matchers.is(Matchers.instanceOf(String.class)),
Matchers.is("2")
)).perform(click());
// 驗證點擊結果
onView(withId(R.id.main_change)).check(matches(withText("2")));
}
}

基礎用法到此已經足夠應付日常測試,更高階的 IdleResource、非同步測試等進階主題則可再另行深入。更多討論也可參考 Stack Overflow 上的 android-testing 標籤。
設計模式之狀態模式,以及 Android StateMachine 源碼透視
定義 「狀態模式允許一個對象在其內部狀態改變時改變其行為,這個對象看起來就像換了個類。」——《JAVA與模式》 管理者持有多個狀態對象,狀態切換後再調用同一個方法,會
Android Button Maker:線上生成 shape 按鈕 XML 的神器
做 Android 介面時,經常需要自訂按鈕的樣式。手寫 shape XML 有點繁瑣,而 Android Button Maker 這個線上工具能讓你透過圖形化面板調整
由淺入深解析 Android Service(一)
一、Service 是什麼 Service 是 Android 四大組件之一,在實際開發中扮演著舉足輕重的角色。先看官方對它的定義: A Service is an ap
Android 自定義 ViewGroup 佈局:onMeasure 與 onLayout 的實作要點
當系統內建的 LinearLayout、RelativeLayout 都無法滿足排列需求時,就得自己繼承 ViewGroup 寫一個佈局。自定義 ViewGroup 的核