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

UI Automator 入門:Android 黑盒 UI 自動化測試環境與流程

編輯:關於 Android 程式設計

Android 的自動化測試框架不少,UI Automator 是 Google 官方提供的黑盒 UI 測試工具,用 Java 撰寫用例。它最大的特色是不需要被測應用的原始碼,因此可以跨應用操作——撥號、發簡訊、開設定都難不倒它。以下記錄一次照官方範例跑完整流程時踩到的坑。

環境準備

  • JDK(是的,沒看錯,寫測試用例還是需要它)與環境變數;
  • Android Studio 或 ADT(IDE 依個人喜好);
  • Android SDK 並設定好 ANDROID_HOME;
  • ANT:用來 build 測試腳本、產出 jar 檔。

ANT 的安裝步驟:

  • 下載並解壓 ANT;
  • 新增系統環境變數 ANT_HOME,值為 ANT 的安裝目錄;
  • 在 Path 中加入 %ANT_HOME%\bin;
  • 在命令列執行 ant -version,若顯示版本資訊即代表設定成功。

使用流程

1. 建立 Java 專案,並掛上 JUnit、對應平台的 android.jar 與 uiautomator.jar:

2. 建立套件(此處以 com 為例)。

3. 撰寫測試類,繼承 UiAutomatorTestCase。以下程式碼來自官方文件,展現了幾個最常見的 API:按 Home、以 description 定位、點擊、處理可滾動容器、驗證畫面:

package com;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Runer extends UiAutomatorTestCase {

    public void testDemo() throws UiObjectNotFoundException {
        // 回到桌面
        getUiDevice().pressHome();

        // 用 content-description 定位「Apps」按鈕
        UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
        allAppsButton.clickAndWaitForNewWindow();

        // 進入 Apps 分頁
        UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
        appsTab.click();

        // 容器可滾動,用 UiScrollable 水平滑動尋找目標
        UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
        appViews.setAsHorizontalList();

        UiObject settingsApp = appViews.getChildByText(
                new UiSelector().className(android.widget.TextView.class.getName()), "Settings");
        settingsApp.clickAndWaitForNewWindow();

        // 驗證是否真的進入設定(比對套件名稱)
        UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.android.settings"));
        assertTrue("Unable to detect Settings", settingsValidation.exists());

        UiObject reportBug = new UiObject(new UiSelector().text("Sound"));
        reportBug.clickAndWaitForNewWindow();

        UiObject soundValidation = new UiObject(new UiSelector().text("Volumes"));
        assertTrue("Unable to detect Sound", soundValidation.exists());

        getUiDevice().pressHome();
    }
}

4. 用 ANT 產生 build.xml。這裡我遇到一個錯誤:

build.xml:26: Class not found: javac1.8

原因是 ADT 自帶的 ANT 插件與本機 JDK 版本不相容,下載新版 ANT(https://ant.apache.org/bindownload.cgi)即可解決。

下載 tar.gz 解壓後,把 bin 目錄加進 Path。接著在 SDK 的 tools 目錄下執行 android list,記住你要執行的平台 ID(也就是當初放 android.jar 與 uiautomator.jar 的那個 platforms):

android create uitest-project -n <name> -t <android-sdk-ID> -p <path>

三個參數:-n 是輸出的 jar 名稱(自訂)、-t 是剛才查到的平台 ID、-p 是 Java 專案所在路徑。例如:

android create uitest-project -n AutoRunner -t 1 -p D:\myAndroidStudy\androidTest

進入該目錄執行 ant build 產生 AutoRunner.jar:

5. 把 jar 推到裝置或模擬器:

adb push AutoRunner.jar /data/local/tmp

6. 執行測試:

adb shell uiautomator runtest AutoRunner.jar -c com.Runer

我的用例在官方範例上多加了點擊「Sound」與返回桌面的動作:

UiObject reportBug = new UiObject(new UiSelector().text("Sound"));
reportBug.clickAndWaitForNewWindow();

UiObject soundValidation = new UiObject(new UiSelector().text("Volumes"));
assertTrue("Unable to detect Sound", soundValidation.exists());

getUiDevice().pressHome();

這段程式碼本身只是個玩具等級的範例,但官方用它帶出了幾個最核心的介面,值得作為起點。

總結

優點:

  • 幾乎所有 UI 操作都能自動化,API 直覺、上手快;
  • 不需要對被測 App 重新簽名,也能操作系統級應用(撥號、簡訊、設定等);
  • 控件定位方式比 Robotium 略簡單一些。

限制:

  • 需要 API Level 16 以上(uiautomator 從 16 才內建);
  • 想用 resource-id 定位控件,需要 API Level 18 以上;
  • 對中文的支援不佳(並非完全不行,可搭配第三方方案);
  • 控件定位的層級不如 Robotium 分明,且它屬於「使用行為注入」而非「原始碼插樁」,測試粒度與能看到內部狀態的程度不同。

補充一個現況:Android 6.0 之後 ADT 與 UiAutomatorTestCase 已被淘汰,現行做法是在 Gradle 中加入 androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.x',改用 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) 取得裝置物件,並以 BySelector(如 By.text()、By.res())取代舊的 UiSelector,建置與執行都交給 Gradle 與 connectedAndroidTest 任務,不再需要手動組 jar 與 ANT。上面的流程仍有助於理解原理,但實際專案請走 AndroidX Test 這條路。

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