標籤:自動化測試 android單元測試 uiautomator unit test 單元測試
在之前的系列文章中,我介紹過用java來實現過 Android 自動化測試(1)如何安裝和卸載一個應用(java)、Android 自動化測試(2)根據ID尋找對象(java);然後又介紹了用python語言來實現Android 自動化測試(3) 根據ID尋找對象&touch&type (python)。還說過後續要寫點關於單元測試和代碼覆蓋測試的文章。今天要介紹的就是單元測試。
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());// 回到HOMEdevice.pressHome();sleep(1000);// 啟動計算機Apptry {Runtime.getRuntime().exec("am start -n com.android.calculator2/.Calculator");} catch (IOException e) {// TODO Auto-generated catch blocke.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 Unit Test & Android Code Coverage Test
Android 自動化測試(4)<uiautomator> 單元測試