使用uiautomatorviewer和uiautomator來做android的UI測試


 

來自:http://university.utest.com    作者:Angelos Nakulas (All Authored Courses)      譯者:Elaine00

目錄

  • 簡介
  • 什么是 Android UI 測試?
  • 如何使用uiautomatorviewer來掃描和分析UI組件
  • 一步一步帶你學uiautomatorviewer
  • 如何使用uiautomator API來創建用戶界面測試
  • 源碼

簡介

 Android UI 測試有兩種測試類型, 其中一種白盒測試需要測試人員了解到應用的源碼。白盒測試,測試人員可以使用為Android應用設計測試用例。第二類被稱為黑盒測試,其中測試人員沒有訪問應用程序的源代碼,但是你需要測試應用程序的行為,當它運行在一台設備

什么是Android UI測試?

在Android用戶界面測試,你要測試的應用程序如何與一個真實的用戶交互。UI測試確保應用程序返回響應用戶一系列操作的正確的UI輸出,如鍵盤輸入或按工具欄,菜單,對話框,圖像,和其他用戶界面控件。為了實現這一目標,你有兩個選擇。一是在真實的應用中來使用這個應用,並且嘗試各種途徑來發現應用的異常表現和Bugs。這種做法顯然是費時,容易出錯,你可以嘗試有限的情況下

另一種方案是自動化測試。自動化測試涉及到創建的程序來完成測試任務(測試用例覆蓋特定的使用場景,然后使用測試框架來自動地執行測試用例和可重復的方式

如何使用uiautomatorviewer來掃描和分析UI組件

第一個工具我們要看到的是uiautomatorviewer,一個用來來掃描和分析Android應用程序的UI組件的GUI工具。為了使用uiautomatorviewer,你必須首先下載並安裝SDK和根據指示設置ADT束Eclipse IDE。安裝后,該工具存在於/tools/文件夾,您可以從命令行輸入啟動它:uiautomatorviewer

但是這個工具的用途是什么?使用uiautomatorviewer,你可以檢查一個應用的UI來查看應用的布局和組件以及相關的屬性。這是非常重要的,特別是當你想構建自動化測試,通過了解各個部件的應用層次的布局,你可以使用uiautomator(在本課程的一部分API)來創建自動化測試

一步一步帶你學uiautomatorviewer

下面我將一步一步帶教你如何使用uiautomatorviewer來查看Calculator應用的布局和組件的ID

  1. 首先通過USB接口連接你的設備到PC端:通過“Settings -> Developer options” 打開 USB調試.
    screen1
  2. 打開Calculator應用。
    screen2
  3. 通過命令行,輸入: uiautomatorviewer (如果路徑有問題 ,你可以切換到  /tools/ 目錄),這個將打開uiautomatorviewer.
    screen3
  4. 通過uiautomatorviewer窗體點擊 “Device screenshot” (查看上面.)
  5. 一個計算機應用的打開窗口將顯示出來
    screen4
  6. 下面將來查看按鈕“7”的ID:點擊  “7” (下面第一個截圖) ,查看右邊的panel來找到 “resource-id” 的值 (下面第二個截圖).
    screen5

    screen6
    現在你知道了:

    • 數字“7”的ID ( “com.android.calculator2:id/digit7”).
    • 如上我們找到數字 “1”的ID (“com.android.calculator2:id/digit1”) 
    • 找到加號“+”的ID  (“com.android.calculator2:id/plus”) 
    • 找到 “=”的ID (“com.android.calculator2:id/equal”)
  7. 同時需要注意的是,當你鼠標懸停在應用程序的屏幕的右上方,uiautomatorviewer面板顯示視圖的層次結構和右下面板顯示鼠標下的部件的更多信息
    screen8

    你現在知道如何使用uiautomator創建一個擁有簡單的功能的用戶界面測試。在下面的部分中,您將構建一個功能性的UI測試,可以檢查應用程序當您添加數字“7”和“1”如何工作。

如何使用uiautomator API來創建用戶界面測試

With uiautomator API, you can create functional UI tests without the need to have access to the source code of the application. The uiautomator is a Java library containing APIs to create customized functional UI tests, and an execution engine to automate and run the tests.

使用uiautomator API,您可以創建功能的UI測試並且不需要訪問應用程序的源代碼。uiautomator 是一個包含java類庫的API,可以用來創建定制功能的UI測試並且有一個自動化引擎來執行測試。

要運行uiautomator, 你必須:

      1. 測試前需要先在測試設備上面安裝app.
        • 現在通過以下步驟來分析app的UI組件. 記錄下你需要測試的組件的屬性 (比如, resource-id, 組件的文字, etc).
      2. Create automated tests to simulate specific user interactions on your application.
          • 使用Eclipse,創建一個新的java項目(例如, “MyFirstTest” ).
          • 在項目中你需要導入uiautomator相關的類庫. 右鍵點擊項目 “Properties” select “Java build path” -> “Libraries” -> “Add library” -> “JUnit” -> “Next” -> “Finish”.

            同樣可以從 “Properties” 選擇“Java build path” -> “Libraries” -> “Add external JARs” ,在platforms目錄下選擇最新的SDK版本和 uiautomator.jar文件,android.jar文件.
            screen10

          • 使用 uiautomator API, 書寫代碼(CalculatorTest.java)來測試 Calculator應用.在uiautomator API 下面有很多的java類,你可以使用這些類來寫測試用例.你寫的 java測試類 (CalculatorTest.java) 必須繼承UiAutomatorTestCase, UiAutomatorTestCase是一個提供多種有用方法的基礎類,你可以使用API的類來寫測試方法測試你的程序如何工作。

        注:API中很多類,API中三個最重要的類

      3. UiDevice -這類表示測試程序開始的時候必須調用getuidevice()這個方法。當你有一個設備的引用,你可以做很多事情了。例如,您可以使用getuidevice().takescreenshotstorepath)實現設備的截圖。你可以使用getUiDevice().pressHome()來點擊“Home” getUiDevice().sleep()來使得設備休眠了

        UiSelector - 通過這個類,你可以定義你需要測試的布局元素,篩選屬性例如as text value, content-description, class名字和狀態。例如,獲取帶有文字 “Bluetooth”的控件 , UiObject button = new UiObject(new UiSelector().text(“Bluetooth”)); 你也可以選擇一個組件通過類名,描述和ID。

        UiObject - 這個類表示的是簡單的UI組件,一旦引用,你可以通過點擊代碼: UiObject seven = new UiObject(new UiSelector().resourceId(“com.android.calculator2:id/digit7″)); seven.click();, 你可以讀取文字:seven.getText();, 或者你可以設置文字, 但是需要先清除文字之后再設置 seven.setText();.

      4. 編譯你的測試用例一個JAR文件,並且把它安裝在您的測試裝置您的應用程序

        在寫完測試場景的代碼之后,你必須按照以下步驟來 編譯你的代碼,創建jar文件,並且把文件導入設備。
        1. 查看/sdk/tools/目錄. (例如: cd /home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/tools/ )
        2. 輸入 /tools/android create uitest-project -n -t 5 -p
        (例如: android create uitest-project -n MyFirstTest -t 5 -p /home/angelos/workspace/MyFirstTest/) 注:

        為了尋找參數t的正確值,列出Android目標列表
        3. Windows 輸入: set ANDROID_HOME= <path_to_your_sdk>.
        Linux 輸入: export ANDROID_HOME= <path_to_your_sdk>.
        (例如: export ANDROID_HOME=/home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/ )
        4. 找到你項目的目錄 (例如: cd /home/angelos/workspace/MyFirstTest/ )
        5. 輸入ant build.
        6. 找到 platform-tools目錄. (Example: cd /home/angelos/Desktop/adt-bundle-linux-x86-20131030/sdk/platform-tools )
        7.把jar導入你的設備(Example: adb push /home/angelos/workspace/MyFirstTest/bin/MyFirstTest.jar /data/local/tmp/ )

      5. 運行測試並且查看測試結果

        為了可以正常運行測試代碼,需要先在控制台: adb shell uiautomator runtest MyFirstTest.jar -c nak.test.CalculatorTest. 注: 名字需要與項目名和類名相同 MyFirstTest.jarnak.test.CalculatorTest. Voila! 接着測試執行過程中可以查看7和1的按鈕是發生什么變化的。

      6. 注意任何錯誤或測試中發現的缺陷。

        現在,你要注意下應用的測試或測試的極端條件下的應用程序的不良表現。你可以寫一個你所可以想到的測試場景

 

源碼

package nak.test;

 

//Import the uiautomator libraries

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

 

public class CalculatorTest extends UiAutomatorTestCase {   

 

public void testingCalculator() throws UiObjectNotFoundException {   

   

// First we testing the press of the HOME button.

getUiDevice().pressHome();

   

 // using the uiautomatorviewer tool we found that the button for the "applications" has

 //the value “Apps” (screen9)

 // so we use this property to create a UiSelector to find the button. 

UiObject Applications = new UiObject(new UiSelector().description("Apps"));

   

// testing the click to bring up the All Apps screen.

Applications.clickAndWaitForNewWindow();

   

// In the All Apps screen, the "Calculator" application is located in 

// the Apps tab. So, we create a UiSelector to find a tab with the text 

// label “Apps”.

UiObject apps = new UiObject(new UiSelector().text("Apps"));

   

// and then testing the click to this tab in order to enter the Apps tab.

apps.click();

 

// All the applications are in a scrollable list 

// so first we need to get a reference to that list

UiScrollable ListOfapplications = new UiScrollable(new UiSelector().scrollable(true));

      

// and then trying to find the application    

// with the name Calculator

UiObject Calculator = ListOfapplications.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()),"Calculator");

Calculator.clickAndWaitForNewWindow();

// now the Calculator app is open

// so we can test the press of button "7" using the ID "com.android.calculator2:id/digit7"

//we found by using uiautomatorviewer

   

UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));

seven.click();

 

// now we test the press of button "+"

UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));

plus.click();

 

// and then the press of button "1"

UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));

one.click();

   

// we test the press of button "="

UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));

result.click();

 

//and finally we test the press of "Back" button

getUiDevice().pressBack();

}   

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM