1. 程式人生 > >Android 自動化測試(4)

Android 自動化測試(4)

     1、 概要

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

      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文字屬性的介面元素。 

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例項:
UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” )); 
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元素。若發現匹配,並且該元素啟用,框架會模擬用
戶的在該元素上的點選操作。 

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

還可以限制搜尋在幾個特定的類中尋找元素,例如,為發現Button類的匹配:
UiObject  cancelButton  =   new   UiObject ( new   UiSelector (). text ( “Cancel” ) .className ( “android.widget.Button” )); 
UiObject  okButton  =   new   UiObject ( new   UiSelector (). text ( “OK” ) .className ( “android.widget.Button” ));
      3、 詳細樣例,注意類庫中需要引用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) {
			// TODO Auto-generated catch block
			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());

		sleep(100);

		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"));
	}
}


      4、總結一下

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