- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動(Activity)
- Android - 服務(Service)
- Android - 廣播接收器(Broadcast Receiver)
- Android - 內容提供器(Content Provider)
- Android - 碎片(Fragment)
- Android - 意圖/過濾器(Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - 音訊管理器
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 影像特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入動畫
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - UI 測試
Android SDK 提供以下工具來支援對您的應用程式進行自動化的、功能性的 UI 測試。
- uiautomatorviewer
- uiautomator
uiautomatorviewer
一個 GUI 工具,用於掃描和分析 Android 應用程式的 UI 元件。
uiautomatorviewer 工具提供了一個方便的視覺介面,用於檢查佈局層次結構並檢視測試裝置上顯示的各個 UI 元件的屬性。 使用此資訊,您以後可以使用選擇器物件建立 uiautomator 測試,這些物件以特定 UI 元件為目標進行測試。
要分析要測試的應用程式的 UI 元件,請在安裝示例中提供的應用程式後執行以下步驟。
- 將您的 Android 裝置連線到開發機器
- 開啟終端視窗並導航到 <android-sdk>/tools/
- 使用此命令執行工具
uiautomatorviewer
命令將按如下所示執行
您將看到以下窗口出現。 這是 UI Automator Viewer 的預設視窗。
點選右上角的裝置圖示。 它將開始獲取當前在裝置中開啟的螢幕的 UI XML 快照。 它將類似於這樣。
之後,您將在 uiautomatorviewer 視窗中看到裝置螢幕的快照。
在此視窗的右側,您將看到兩個分割槽。 上部分割槽解釋了節點結構,即 UI 元件的排列和包含方式。 點選每個節點將在下部分割槽中顯示詳細資訊。
例如,考慮下圖。 當您點選按鈕時,您可以在上部分割槽中看到已選擇 Button,在下部分割槽中顯示其詳細資訊。 由於此按鈕是可點選的,因此其可點選屬性設定為 true。
UI Automator Viewer 還可幫助您檢查不同方向的 UI。 例如,只需將裝置方向更改為橫向,然後再次捕獲螢幕截圖。 如下圖所示 -
uiautomator
現在您可以建立自己的測試用例並使用 uiautomatorviewer 執行它來檢查它們。 為了建立自己的測試用例,您需要執行以下步驟 -
從專案資源管理器中,右鍵點選您建立的新專案,然後選擇屬性>Java 構建路徑,並執行以下操作 -
點選新增庫>JUnit,然後選擇 JUnit3 以新增 JUnit 支援。
點選新增外部 JAR... 並導航到 SDK 目錄。 在平臺目錄下,選擇最新的 SDK 版本並新增 uiautomator.jar 和 android.jar 檔案。
使用 UiAutomatorTestCase 擴充套件您的類
編寫必要的測試用例。
完成測試程式碼編寫後,請按照以下步驟構建並將測試 JAR 部署到目標 Android 測試裝置。
建立構建輸出 JAR 所需的構建配置檔案。 要生成構建配置檔案,請開啟終端並執行以下命令
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
<name> 是包含 uiautomator 測試原始檔的專案的名稱,<path> 是相應專案目錄的路徑。
從命令列設定 ANDROID_HOME 變數。
set ANDROID_HOME=<path_to_your_sdk>
- 轉到包含 build.xml 檔案的專案目錄並構建測試 JAR。
ant build
- 使用 adb push 命令將生成的測試 JAR 檔案部署到測試裝置。
adb push <path_to_output_jar> /data/local/tmp/
- 按照以下命令執行您的測試 -
adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings
示例
以下示例演示了 UITesting 的用法。 它建立了一個基本應用程式,可用於 uiautomatorviewer。
要試驗此示例,您需要在實際裝置上執行它,然後按照開頭解釋的 uiautomatorviewer 步驟操作。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio 在包 com.tutorialspoint.myapplication 下建立一個 Android 應用程式。 |
| 2 | 修改 src/MainActivity.java 檔案以新增 Activity 程式碼。 |
| 3 | 修改佈局 XML 檔案 res/layout/activity_main.xml,根據需要新增任何 GUI 元件。 |
| 4 | 建立 src/second.java 檔案以新增 Activity 程式碼。 |
| 5 | 修改佈局 XML 檔案 res/layout/view.xml,根據需要新增任何 GUI 元件。 |
| 6 | 執行應用程式並選擇一個正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果。 |
以下是 MainActivity.java 的內容。
package com.tutorialspoint.myapplication;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in =new Intent(MainActivity.this,second.class);
startActivity(in);
}
});
}
}
以下是 second.java 的內容。
package com.tutorialspoint.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Button b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(second.this,"Thanks",Toast.LENGTH_LONG).show();
}
});
}
}
以下是 activity_main.xml 的內容
在以下程式碼中,abc 表示 tutorialspoint.com 的徽標
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UI Animator Viewer"
android:id="@+id/textView"
android:textSize="25sp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:textColor="#ff36ff15"
android:textIsSelectable="false"
android:textSize="35dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_marginTop="98dp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是 view.xml 的內容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是 Strings.xml 的內容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 的內容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorialspoint.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
讓我們嘗試執行您的 UI 測試應用程式。 我假設您已將您的實際 Android 手機裝置連線到您的計算機。 要從 Android Studio 執行該應用程式,請開啟專案的 Activity 檔案之一,然後點選工具欄中的執行
圖示。 在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇您要執行 Android 應用程式的位置。
選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示應用程式螢幕。 現在只需按照頂部 ui 自動化檢視器部分中提到的步驟操作,即可對該應用程式執行 ui 測試。
