Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Espresso UI 測試完全入門

編輯:關於 Android 程式設計

前言

Espresso 是 Google 官方推出的 Android 界面測試框架,它能夠讓我們以接近「用戶真實操作」的方式去驅動和驗證 App 的 UI。本文整理自官方文檔的核心概念,並附上實作步驟與範例,方便快速上手。

Espresso 的四大核心組成

理解 Espresso,先要記住以下四個關鍵角色:

  • Espresso:測試的入口,透過 onView() 與 onData() 與視圖互動,其 API 不依賴任何具體的 View。
  • ViewMatchers:一組實現了 Matcher<View> 的集合對象,用來定位當前的 View。
  • ViewActions:包含各種操作方法(例如點擊),透過 ViewInteraction.perform() 觸發。
  • ViewAssertions:用於斷言並檢查 View 的狀態,透過 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 來精準定位。

步驟二:執行 Action

在匹配到目標 View 後,透過 perform() 執行操作:

// 單一點擊
onView(withId(R.id.main_view)).perform(click());
// 一次執行多個操作
onView(...).perform(typeText("Hello"), click());
// 在 ScrollView 中先捲動再點擊
onView(...).perform(scrollTo(), click());

注意操作會依序執行,例如 click() 會在 scrollTo() 之後才觸發。

步驟三:檢查 Assertion

透過 check() 執行斷言,通常搭配 matches() 使用:

// 檢查 View 是否包含 "Hello" 內容
onView(...).check(matches(withText("Hello!")));

需要注意的是,若 View 是否顯示尚無法確定,不宜把 isDisplayed() 這類斷言直接放進定位環節,否則容易誤判。應先確認 View 已顯示,再進行內容斷言。

在 IDE 中開始編寫

Eclipse

在 Eclipse 中主要是加入對應的靜態 jar 包。官方在 GitHub 上同時提供了 Android Studio 與 Eclipse 的範例專案,使用 Eclipse 時選擇 BasicSampleBundled 即可。

Android Studio

第一步:加入 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 標籤。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved