• Android Video Tutorials

Android - UI 測試



Android SDK 提供以下工具來支援對您的應用程式進行自動化的、功能性的 UI 測試。

  • uiautomatorviewer
  • uiautomator

uiautomatorviewer

一個 GUI 工具,用於掃描和分析 Android 應用程式的 UI 元件。

uiautomatorviewer 工具提供了一個方便的視覺介面,用於檢查佈局層次結構並檢視測試裝置上顯示的各個 UI 元件的屬性。 使用此資訊,您以後可以使用選擇器物件建立 uiautomator 測試,這些物件以特定 UI 元件為目標進行測試。

要分析要測試的應用程式的 UI 元件,請在安裝示例中提供的應用程式後執行以下步驟。

  • 將您的 Android 裝置連線到開發機器
  • 開啟終端視窗並導航到 <android-sdk>/tools/
  • 使用此命令執行工具
uiautomatorviewer

命令將按如下所示執行

Android UI Testing Tutorial

您將看到以下窗口出現。 這是 UI Automator Viewer 的預設視窗。

Android UI Testing Tutorial
  • 點選右上角的裝置圖示。 它將開始獲取當前在裝置中開啟的螢幕的 UI XML 快照。 它將類似於這樣。

Android UI Testing Tutorial

之後,您將在 uiautomatorviewer 視窗中看到裝置螢幕的快照。

Android UI Testing Tutorial

在此視窗的右側,您將看到兩個分割槽。 上部分割槽解釋了節點結構,即 UI 元件的排列和包含方式。 點選每個節點將在下部分割槽中顯示詳細資訊。

例如,考慮下圖。 當您點選按鈕時,您可以在上部分割槽中看到已選擇 Button,在下部分割槽中顯示其詳細資訊。 由於此按鈕是可點選的,因此其可點選屬性設定為 true。

Android UI Testing Tutorial

UI Automator Viewer 還可幫助您檢查不同方向的 UI。 例如,只需將裝置方向更改為橫向,然後再次捕獲螢幕截圖。 如下圖所示 -

Android UI Testing Tutorial

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 檔案之一,然後點選工具欄中的執行 Eclipse Run Icon 圖示。 在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇您要執行 Android 應用程式的位置。

Anroid UI Testing Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示應用程式螢幕。 現在只需按照頂部 ui 自動化檢視器部分中提到的步驟操作,即可對該應用程式執行 ui 測試。

廣告

© . All rights reserved.