編輯:關於 Android 程式設計
UI Automator 是 Android 官方提供的跨應用 UI 測試框架,它能夠模擬使用者在裝置上的操作,也能讀取目前畫面上的元件資訊,適合用來寫「打開某個 App、點擊某顆按鈕、驗證結果」這類端到端測試。本文記錄在 Android Studio 中把 UI Automator 跑起來的完整過程。
UI Automator 以 Support Library 的形式發布,因此必須先透過 SDK Manager 安裝 Android Support Repository,否則後續在 build.gradle 中加入依賴時會找不到套件。

minSdkVersion 18
在 Module 層級的 build.gradle 中宣告對 UI Automator 的依賴。注意它屬於 androidTestCompile,也就是只會被打進測試 APK,不會進入正式版:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
// UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}
先寫一個最簡單的 demo:畫面上有一個 TextView 和一顆 Button,每按一次按鈕,文字就累加一次點擊次數。這樣後續測試就有明確的驗證目標。
版面配置 activity_main.xml:
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.test.myapplication.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginStart="14dp"
android:text="Button" />
</RelativeLayout>
Activity 程式碼:
private int clicks = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("click: " + clicks);
clicks++;
}
});
}
在 androidTest 資料夾底下新增測試類別。

測試程式碼:
public class MainTest extends UiAutomatorTestCase {
public void testMain() throws UiObjectNotFoundException {
// 以系統計算機為例的操作示範(已註解)
// getUiDevice().pressHome();
// UiObject Calculator = new UiObject(new UiSelector().description("計算器"));
// Calculator.clickAndWaitForNewWindow();
// UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));
// seven.click();
// UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));
// plus.click();
// UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));
// one.click();
// UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));
// result.click();
// getUiDevice().pressBack();
getUiDevice().pressHome();
Context context = InstrumentationRegistry.getContext();
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName(
"app.test.myapplication", "app.test.myapplication.MainActivity"));
context.startActivity(launchIntent);
UiObject main = new UiObject(new UiSelector()
.resourceId("app.test.myapplication:id/button"));
main.click();
}
}
第一,UiAutomatorTestCase 是舊版 API 的用法。若採用 uiautomator-v18:2.1.0 這一代的函式庫,建議改用 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) 取得裝置物件,測試類別則以 JUnit 的方式撰寫,不再需要繼承 UiAutomatorTestCase。
第二,UiSelector 的定位順序建議優先使用 resourceId,其次才是 text 或 description。resourceId 穩定度最高,而文字很容易因為多語言或文案調整而失效。
第三,UI Automator 只能「看到」目前畫面上的元件,無法存取元件背後的內部狀態。若需要更精細的介面驗證,可以搭配 Espresso 一起使用。
如果執行時出現 UiObjectNotFoundException,通常代表定位條件寫錯,或者目標元件所在的頁面還沒切換過去。建議先在測試中插入 getUiDevice().wait() 或 UiObject2.waitForExists(),確保畫面已經就緒再操作。

更多說明可參考官方文件中的 UI Automator 章節。
Android 圖片快取實作:LruCache 與 DiskLruCache 雙層策略
在 App 中載入網路圖片時,若每次都重新下載,不僅浪費流量,也會拖慢使用者體驗。常見的解法是加入快取:先查記憶體,再查本機檔案,最後才走網路。Android 中常用 L
Android 在非 UI 線程更新介面的兩種常見做法
為什麼不能在非 UI 線程直接更新介面 在 Android 中,UI 元件通常只能在主線程,也就是 UI 線程中操作。如果在子線程中直接更新 TextView、Toast
Android HandlerThread 全面解析:Looper、Message 與 IntentService
在 Android 中,UI 更新通常需要在主執行緒進行,但耗時工作若也放在主執行緒,容易造成畫面卡頓。HandlerThread 的價值,就是把 Looper 與 Ha
Android Studio 中透過 JNI 調用 C 代碼的完整實踐
一、搭建 NDK 開發環境 第一步:在 Android Studio 中指定 NDK 路徑 JNI 開發的起點是把 NDK 工具集準備好。開啟 File → Projec