UIAutomator教程

轉自http://blog.csdn.net/vshuang/article/details/40210163

在之前的系列文章中,我介紹過用java來實現過 Android 自動化測試(1)如何安裝和卸載一個應用(java)Android 自動化測試(2)根據ID查找對象(java);然後又介紹了用python語言來實現Android 自動化測試(3) 根據ID查找對象&touch&type (python)。還說過後續要寫點關於UI測試和代碼覆蓋測試的文章。今天要介紹的就是UI測試。

     1、 概要

      做過java單元測試的同學,使用Android的單元測試比較簡單,參見 如何進行Android單元測試,採用這種方式,業務邏輯上的測試就解決了。只是有一個明顯的缺陷就是測試界面不方便。而對於android應用程序來說,界面佔據了很重要的一個部分。

      這個時候可以使用uiautomator.jar這個類庫。  這裏我不詳細講具體的Android 的 uiautomator類庫怎麼使用。具體的使用可以參見Android UI Testing (英文版), 和 Android uiautomator 使用入門官方教程(中文版)

      2、核心類

      我主要提一下里面最重要的核心類UiDevice、UISelector和UiObject ,如何查找對象和作用於對象是測試的核心。

      UiDevice

      Represents the device state. In your tests, you can call methods on the  UiDevice  instance to check for the state of various properties, such as current orientation or display size. Your tests also can use the  UiDevice  instance to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons.
      UiDevice代表設備狀態。在測試時,可以調用UiDevice實例的方法來檢查不同屬性的狀態,如當前的屏幕旋轉方向貨展示大小。測試代碼還能使用UiDevice實例來執行設備級的操作,如強制設備橫豎屏,按壓d-pad硬件按鈕,或按壓主屏幕鍵和菜單鍵。
      獲取UiDevice實例,模擬按壓主屏幕鍵的代碼如下: getUiDevice (). pressHome ();

     UiSelector 
      Represents a search criteria to query and get a handle on specific elements in the currently displayed UI. If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. When constructing a  UiSelector , you can chain together multiple properties to refine your search. If no matching UI element is found, a UiAutomatorObjectNotFoundException  is thrown. You can use the  childSelector()  method to nest multiple  UiSelector  instances. For example, the following code example shows how to specify a search to find the first  ListView  in the currently displayed UI, then search within that  ListView  to find a UI element with the text property Apps. 
      UiSelector代表一種搜索標準,可以在當前展示界面上查詢和獲取特定元素的句柄。若找到多於一個的匹配元素,則返回佈局層次結構上的第一個匹配元素作爲目標UiObject。當構造一個UiSelector對象時,可以使用鏈式調用多個屬性來縮小查詢範圍。如無匹配元素,則返回異常 UiAutomatorObjectNotFoundException 。你還可以使用 childSelector()  方法來嵌套多個Uiselector實例。例如。下面的代碼演示如何制定查詢來定位在當前界面的第一個ListView,然後在返回的ListView內定位一個帶有Apps文本屬性的界面元素。 

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. UiObject  appItem  =   new   UiObject ( new   UiSelector () . className ( “android.widget.ListView” ). instance ( 1 ) . childSelector ( new   UiSelector (). text ( “Apps” )));  
      UiObject 
      Represents a UI element. To create a  UiObject  instance, use a UiSelector that describes how to search for, or select, the UI element.
The following code example shows how to construct  UiObject  instances that represent a Cancel button and a OKbutton in your application.
UiObject代表一個UI元素。爲創建一個UiObject實例,使用用來描述如何搜索、選定UI元素的UiSelector實例:
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” ));   
  2. UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ));  

      You can reuse the  UiObject  instances that you have created in other parts of your app testing, as needed. Note that the uiautomator test framework searches the current display for a match every time your test uses a  UiObject instance to click on a UI element or query a property.
      In the following code example, the uiautomator test framework searches for a UI element with the text property OK. If a match is found and if the element is enabled, the framework simulates a user click action on the element.You can also restrict the search to find only elements of a specific class. For example, to find matches of the  Button class:
      必要時,可以重用測試項目中已經創建的UiObject實例。注意,測試用例每次使用UiObject實例來點擊UI元素或查詢屬性時,uiautomator測試框架會搜索當前的界面來尋找匹配。在下面的代碼中,uiautomator測試框架搜索帶有OK文本屬性的UI元素。若發現匹配,並且該元素啓用,框架會模擬用戶的在該元素上的點擊操作。  

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. if ( okButton . exists ()   &&  okButton . isEnabled ()) {  
  2.     okButton . click ();  
  3. }  

還可以限制搜索在幾個特定的類中尋找元素,例如,爲發現Button類的匹配:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” ) .className ( “android.widget.Button” ));   
  2. UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ) .className ( “android.widget.Button” ));  


      3、 詳細樣例,注意類庫中需要引用android.jar和uiautomator.jar包啊

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package xzy.test.uiautomator;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.os.RemoteException;  
  6.   
  7. import com.android.uiautomator.core.UiDevice;  
  8. import com.android.uiautomator.core.UiObject;  
  9. import com.android.uiautomator.core.UiObjectNotFoundException;  
  10. import com.android.uiautomator.core.UiSelector;  
  11. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  12.   
  13. public class CalTest extends UiAutomatorTestCase {  
  14.   
  15.     public void testDemo() throws UiObjectNotFoundException, RemoteException {  
  16.   
  17.         UiDevice device = getUiDevice();  
  18.         // 喚醒屏幕  
  19.         device.wakeUp();  
  20.         assertTrue("screenOn: can't wakeup", device.isScreenOn());  
  21.         // 回到HOME  
  22.         device.pressHome();  
  23.         sleep(1000);  
  24.   
  25.         // 啓動計算器App  
  26.         try {  
  27.             Runtime.getRuntime().exec(  
  28.                     "am start -n com.android.calculator2/.Calculator");  
  29.         } catch (IOException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         }  
  33.   
  34.         sleep(1000);  
  35.         UiObject oneButton = new UiObject(new UiSelector().text("1"));  
  36.         assertTrue("oneButton not found", oneButton.exists());  
  37.         UiObject plusButton = new UiObject(new UiSelector().text("+"));  
  38.         assertTrue("plusButton not found", plusButton.exists());  
  39.   
  40.         sleep(100);  
  41.   
  42.         UiObject equalButton = new UiObject(new UiSelector().text("="));  
  43.         assertTrue("equalButton not found", equalButton.exists());  
  44.   
  45.         oneButton.click();  
  46.         sleep(100);  
  47.         plusButton.click();  
  48.         sleep(100);  
  49.         oneButton.click();  
  50.   
  51.         equalButton.click();  
  52.         sleep(100);  
  53.   
  54.         UiObject switcher = new UiObject(  
  55.                 new UiSelector()  
  56.                         .resourceId("com.android.calculator2:id/display"));  
  57.         UiObject result = switcher.getChild(new UiSelector().index(0));  
  58.         System.out.print("text is :" + result.getText());  
  59.         assertTrue("result != 2", result.getText().equals("2"));  
  60.     }  
  61. }  


      4、總結一下

      單元測試比較簡單,但是很有效,對於要做自動化測試的團隊,和要提供穩定而又質量的交付,單元測試是很重要的噢 。Android已經有一套非常完善的單元測試支持了,UI測試也 OK


      5、後面會介紹一些採用 robotium 框架進行 Android UI Test & Android Code Coverage Test

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章