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

Android 自動化測試(4):UI Automator 實戰

編輯:關於 Android 程式設計

在此前的系列文章中,我介紹過用 Java 實現 Android 自動化測試(如安裝卸載應用、根據 ID 查找對象),也用 Python 講過根據 ID 查找對象、touch 與 type 等操作。今天要介紹的是 UI 測試。

一、概要

做過 Java 單元測試的同學,會發現 Android 的單元測試並不難,它能很好地解決業務邏輯層面的測試;但有一個明顯缺陷——測試界面不方便。而對 Android 應用來說,界面佔據了舉足輕重的位置。這時就可以借助 uiautomator.jar 類庫。關於其具體用法,可參考 Android 官方的 UI Testing 及 uiautomator 入門教程。

二、核心類

其中最重要的核心類是 UiDevice、UiSelector 和 UiObject——如何查找對象並作用於對象,正是測試的核心。

UiDevice:代表設備狀態。測試時可調用其實例的方法檢查各種屬性狀態(如當前屏幕方向、顯示尺寸),還能執行設備級操作,如強制橫豎屏、按壓 d-pad 硬件鍵、Home 鍵與菜單鍵。獲取實例並模擬按 Home 鍵的代碼:

getUiDevice().pressHome();

UiSelector:代表一種搜索條件,用於在當前界面查詢並獲取特定元素的句柄。若匹配多個元素,則返回布局層次上的第一個作為目標 UiObject。構造時可鏈式調用多個屬性來縮小范圍;若無匹配,會拋出 UiAutomatorObjectNotFoundException。還可用 childSelector() 嵌套多個 UiSelector:

UiObject appItem = new UiObject(new UiSelector()
        .className("android.widget.ListView").instance(1)
        .childSelector(new UiSelector().text("Apps")));

UiObject:代表一個 UI 元素。創建時需用描述搜索方式的 UiSelector:

UiObject cancelButton = new UiObject(new UiSelector().text("Cancel"));
UiObject okButton = new UiObject(new UiSelector().text("OK"));

必要時可重用已創建的 UiObject 實例。注意,每次用 UiObject 點擊元素或查詢屬性時,框架都會重新搜索當前界面尋找匹配。如下例,若找到 OK 元素且其處於啟用狀態,框架便會模擬用戶點擊:

if (okButton.exists() && okButton.isEnabled()) {
    okButton.click();
}

還可將搜索限制在特定類中,例如只匹配 Button 類:

UiObject okButton = new UiObject(new UiSelector().text("OK")
        .className("android.widget.Button"));

三、完整樣例

注意:類庫中需引用 android.jar 和 uiautomator.jar 兩個包。以下示例演示喚醒屏幕、啟動計算器並完成一次加法運算:

package xzy.test.uiautomator;

import java.io.IOException;
import android.os.RemoteException;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class CalTest extends UiAutomatorTestCase {
    public void testDemo() throws UiObjectNotFoundException, RemoteException {
        UiDevice device = getUiDevice();
        // 喚醒屏幕
        device.wakeUp();
        assertTrue("screenOn: can't wakeup", device.isScreenOn());
        // 回到 HOME
        device.pressHome();
        sleep(1000);
        // 啟動計算器 App
        try {
            Runtime.getRuntime().exec("am start -n com.android.calculator2/.Calculator");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sleep(1000);
        UiObject oneButton = new UiObject(new UiSelector().text("1"));
        assertTrue("oneButton not found", oneButton.exists());
        UiObject plusButton = new UiObject(new UiSelector().text("+"));
        assertTrue("plusButton not found", plusButton.exists());
        UiObject equalButton = new UiObject(new UiSelector().text("="));
        assertTrue("equalButton not found", equalButton.exists());
        oneButton.click();
        sleep(100);
        plusButton.click();
        sleep(100);
        oneButton.click();
        equalButton.click();
        sleep(100);
        UiObject switcher = new UiObject(new UiSelector()
                .resourceId("com.android.calculator2:id/display"));
        UiObject result = switcher.getChild(new UiSelector().index(0));
        System.out.print("text is :" + result.getText());
        assertTrue("result != 2", result.getText().equals("2"));
    }
}

四、總結

單元測試簡單而有效,對追求穩定與質量的團隊而言十分重要。Android 已有完善的單元測試支持,UI 測試同樣勝任。後續我還會介紹如何採用 robotium 框架進行 Android UI Test 與代碼覆蓋率測試。

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