Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自動化測試(4)(uiautomator)

Android 自動化測試(4)(uiautomator)

編輯:關於Android編程

在之前的系列文章中,我介紹過用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文本屬性的界面元素。

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


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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved