UiAutomator自動化測試框架介紹
環境搭建
1 必要條件
1.1 JDK
1.2 SDK(API高於15)
1.3 Eclipse
2 簡要步驟
2.1 安裝JDK並添加bin目錄到環境變量
2.2 添加SDK中的tools目錄和platform-tools到環境變量
2.3 安裝Eclipse並安裝ADT插件
建立工程
1 用eclipse建立Java project
2 添加Junit庫
3 添加Android庫
找到sdk/platforms/android-19/下面的android.jar和uiautomator.jar並添加進來
4 添加完成后如下圖
在工程中編寫case
1 分析Device當前界面的UI內容
在編寫Case之前,需要找到界面上不同控件的id,text,class或者description等等,來定位具體的點擊事件。我們可以使用uiautomatorviewer工具來分析界面的組織架構。
2 官方用例
package com.uia.example.my;
// Import the uiautomator libraries
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class LaunchSettings extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
getUiDevice().pressHome();
// We’re now in the home screen. Next, we want to simulate
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot
// of the Home screen, notice that the All Apps button’s
// content-description property has the value “Apps”. We can
// use this property to create a UiSelector to find the button.
UiObject allAppsButton = new UiObject(new UiSelector()
.description("Apps"));
// Simulate a click to bring up the All Apps screen.
allAppsButton.clickAndWaitForNewWindow();
// In the All Apps screen, the Settings app is located in
// the Apps tab. To simulate the user bringing up the Apps tab,
// we create a UiSelector to find a tab with the text
// label “Apps”.
UiObject appsTab = new UiObject(new UiSelector()
.text("Apps"));
// Simulate a click to enter the Apps tab.
appsTab.click();
// Next, in the apps tabs, we can simulate a user swiping until
// they come to the Settings app icon. Since the container view
// is scrollable, we can use a UiScrollable object.
UiScrollable appViews = new UiScrollable(new UiSelector()
.scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
UiObject settingsApp = appViews.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
"Settings");
settingsApp.clickAndWaitForNewWindow();
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(new UiSelector()
.packageName("com.android.settings"));
assertTrue("Unable to detect Settings",
settingsValidation.exists());
}
}
Build工程
1 產生build工程所需要的配置文件
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
name: build jar文件的名字
1: 使用命令android list可以查看
path: 工程目錄
2 build工程jar文件
2.1配置文件創建完成后,刷新工程目錄,將會產生三個文件:build.xml、local.properties和project.properties
2.2 打開build.xml,將default值改成build,如下圖
2.3 build工程
運行工程
1 導入
將build好的jar文件,push到手機目錄/data/local/tmp/下
2 運行命令
adb shell uiautomator runtest LaunchSettings.jar(build出來的文件) -c com.uia.example.my.LaunchSettings(class名)
如果想要運行class類里具體的方法,可以在類后面加上#方法名即可
如果想手機單獨運行程序,不受數據線的約束,可以使用--nohup
API詳解
1 UiDevice
提供關於設備的狀態信息。你也可以使用這個類來模擬設備上的用戶的行為,如按方向鍵或者按菜單鍵
如若獲取UiDevice的對象,請使用UiDevice.getInstance()方法
Public Methods |
||
void |
清除上一次輸入的文本 |
|
Clears the text from the last UI traversal event. |
||
boolean |
點擊坐標 |
|
Perform a click at arbitrary coordinates specified by the user |
||
boolean |
從開始坐標拖拽到終點坐標位置 |
|
Performs a swipe from one coordinate to another coordinate. |
||
void |
dump當前界面的層次結構,並保存到/data/local/tmp/目錄下 |
|
Helper method used for debugging to dump the current window's layout hierarchy. |
||
void |
禁用傳感器,並且在當前界面凍結轉屏 |
|
Disables the sensors and freezes the device rotation at its current rotation state. |
||
String |
獲取當前Activity的名字,此方法已被丟棄,不建議使用 |
|
This method is deprecated. The results returned should be considered unreliable |
||
String |
獲取當前包名 |
|
Retrieves the name of the last package to report accessibility events. |
||
int |
獲取顯示高度,以像素為單位 |
|
Gets the height of the display, in pixels. |
||
int |
返回當前的旋轉值,0,90,180,270 |
|
Returns the current rotation of the display, as defined in Surface |
||
Point |
|
|
Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation. |
||
int |
獲取顯示寬度,以像素為單位 |
|
Gets the width of the display, in pixels. |
||
static UiDevice |
Java單例模式,獲取UiDevice的對象 |
|
Retrieves a singleton instance of UiDevice |
||
String |
獲取最后一次輸入的text |
|
Retrieves the text from the last UI traversal event received. |
||
String |
獲取設備的名字 |
|
Retrieves the product name of the device. |
||
boolean |
檢查是否有監聽器觸發過 |
|
boolean |
檢查某個特定的監聽器是否觸發過 |
|
boolean |
檢查設備是否是正常的豎屏狀態 |
|
Check if the device is in its natural orientation. |
||
boolean |
檢查屏幕是否亮起 |
|
Checks the power manager if the screen is ON. |
||
boolean |
打開notification通知欄 |
|
Opens the notification shade. |
||
boolean |
打開quicksetting |
|
Opens the Quick Settings shade. |
||
boolean |
按返回鍵 |
|
Simulates a short press on the BACK button. |
||
boolean |
軌跡球 |
|
Simulates a short press on the CENTER button. |
||
boolean |
軌跡球 |
|
Simulates a short press on the DOWN button. |
||
boolean |
軌跡球 |
|
Simulates a short press on the LEFT button. |
||
boolean |
軌跡球 |
|
Simulates a short press on the RIGHT button. |
||
boolean |
軌跡球 |
|
Simulates a short press on the UP button. |
||
boolean |
刪除鍵 |
|
Simulates a short press on the DELETE key. |
||
boolean |
回車鍵 |
|
Simulates a short press on the ENTER key. |
||
boolean |
home鍵 |
|
Simulates a short press on the HOME button. |
||
boolean |
keycode,參照keyevent |
|
Simulates a short press using a key code. |
||
boolean |
keycode,參照keyevent |
|
Simulates a short press using a key code. |
||
boolean |
menu鍵 |
|
Simulates a short press on the MENU button. |
||
boolean |
recent鍵 |
|
Simulates a short press on the Recent Apps button. |
||
boolean |
search鍵 |
|
Simulates a short press on the SEARCH button. |
||
void |
registerWatcher(String name, UiWatcher watcher) |
注冊一個監聽器,當前指定動作被打斷的時候,處理中斷異常 |
Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. |
||
void |
移除之前注冊的監聽器 |
|
void |
重置一個監聽器 |
|
void |
強制運行所有的監聽器 |
|
This method forces all registered watchers to run. |
||
void |
應用或禁用布局層次壓縮 |
|
Enables or disables layout hierarchy compression. |
||
void |
通過禁用傳感器,然后模擬設備向左轉,並且固定位置 |
|
Simulates orienting the device to the left and also freezes rotation by disabling the sensors. |
||
void |
通過禁用傳感器,然后模擬設備轉到其自然默認的方向,並且固定位置 |
|
Simulates orienting the device into its natural orientation and also freezes rotation by disabling the sensors. |
||
void |
通過禁用傳感器,然后模擬設備向右轉,並且固定位置 |
|
Simulates orienting the device to the right and also freezes rotation by disabling the sensors. |
||
void |
鎖屏 |
|
This method simply presses the power button if the screen is ON else it does nothing if the screen is already OFF. |
||
boolean |
在點陣列中滑動,5秒一步 |
|
Performs a swipe between points in the Point array. |
||
boolean |
swipe(int startX, int startY, int endX, int endY, int steps) |
通過坐標滑動屏幕 |
Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed. |
||
boolean |
當前窗口截屏,並將其存儲為PNG格式默認1.0f的規模(原尺寸)和90%的圖片質量,參數為file類的文件路徑 |
|
Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation |
||
boolean |
當前窗口截屏,可以自定義尺寸和圖片質量 |
|
Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation |
||
void |
重新啟用傳感器,並且允許旋轉屏幕 |
|
Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation. |
||
void |
等待當前應用程序處於空閑狀態 |
|
Waits for the current application to idle. |
||
void |
等待當前應用程序處於空閑狀態 |
|
Waits for the current application to idle. |
||
boolean |
等待窗口內容更新時間的發生 |
|
Waits for a window content update event to occur. |
||
void |
喚醒屏幕 |
|
This method simulates pressing the power button if the screen is OFF else it does nothing if the screen is already ON. |
2 UiSelector
用於按照一定的條件,定位界面上的控件元素。UiObject對象通過UiSelector來選取
Public Constructors |
||
|
構造方法 |
|
Public Methods |
||
UiSelector |
是否是可選擇的屬性 |
|
Set the search criteria to match widgets that are checkable. |
||
UiSelector |
是否已經被選擇 |
|
Set the search criteria to match widgets that are currently checked (usually for checkboxes). |
||
UiSelector |
childSelector(UiSelector selector) |
子類 |
Adds a child UiSelector criteria to this selector. |
||
UiSelector |
類名 |
|
Set the search criteria to match the class property for a widget (for example, "android.widget.Button"). |
||
<T> UiSelector |
泛型類名 |
|
Set the search criteria to match the class property for a widget (for example, "android.widget.Button"). |
||
UiSelector |
類名正則表達式 |
|
Set the search criteria to match the class property for a widget, using a regular expression. |
||
UiSelector |
是否可以被點擊 |
|
Set the search criteria to match widgets that are clickable. |
||
UiSelector |
通過content-description來選取 |
|
Set the search criteria to match the content-description property for a widget. |
||
UiSelector |
通過content-description所包含的字符來選取 |
|
Set the search criteria to match the content-description property for a widget. |
||
UiSelector |
通過content-description所符合正則表達式 的字符來選取 |
|
Set the search criteria to match the content-description property for a widget. |
||
UiSelector |
通過content-description開始字符匹配來選取 |
|
Set the search criteria to match the content-description property for a widget. |
||
UiSelector |
是否已經被enabled |
|
Set the search criteria to match widgets that are enabled. |
||
UiSelector |
焦點屬性 |
|
Set the search criteria to match widgets that are focusable. |
||
UiSelector |
當前焦點屬性 |
|
Set the search criteria to match widgets that have focus. |
||
UiSelector |
fromParent(UiSelector selector) |
父類 |
Adds a child UiSelector criteria to this selector which is used to start search from the parent widget. |
||
UiSelector |
編號 |
|
Set the search criteria to match the widget by its node index in the layout hierarchy. |
||
UiSelector |
索引 |
|
Set the search criteria to match the widget by its instance number. |
||
UiSelector |
是否可以被長按 |
|
Set the search criteria to match widgets that are long-clickable. |
||
UiSelector |
包名 |
|
Set the search criteria to match the package name of the application that contains the widget. |
||
UiSelector |
正則包名 |
|
Set the search criteria to match the package name of the application that contains the widget. |
||
UiSelector |
資源id |
|
Set the search criteria to match the given resource ID. |
||
UiSelector |
資源id正則表達式 |
|
Set the search criteria to match the resource ID of the widget, using a regular expression.http://blog.bettersoftwaretesting.com/ |
||
UiSelector |
是否可以被滑動 |
|
Set the search criteria to match widgets that are scrollable. |
||
UiSelector |
當前選擇屬性 |
|
Set the search criteria to match widgets that are currently selected. |
||
UiSelector |
根據text來選取控件或者應用程序 |
|
Set the search criteria to match the visible text displayed in a widget (for example, the text label to launch an app). |
||
UiSelector |
根據text中包含的字符串 來選取控件或者應用程序 |
|
Set the search criteria to match the visible text in a widget where the visible text must contain the string in your input argument. |
||
UiSelector |
通過正則表達式來選取匹配的控件或者應用程序 |
|
Set the search criteria to match the visible text displayed in a layout element, using a regular expression. |
||
UiSelector |
通過文本開始字符來選取 |
|
Set the search criteria to match visible text in a widget that is prefixed by the text parameter. |
||
String |
|
|
Protected Methods |
||
UiSelector |
克隆UiSelector |
3 UiObject
UiObject主要對對象進行操作
Public Constructors |
||
|
UiObject(UiSelector selector) |
UiObject的構造方法 |
Constructs a UiObject to represent a view that matches the specified selector criteria. |
||
Public Methods |
||
void |
清除編輯框中的文本 |
|
Clears the existing text contents in an editable field. |
||
boolean |
點擊事件 |
|
Performs a click at the center of the visible bounds of the UI element represented by this UiObject. |
||
boolean |
點擊對象,等待新窗口的出現, 參數為等待超時時長 |
|
Performs a click at the center of the visible bounds of the UI element represented by this UiObject and waits for window transitions. |
||
boolean |
點擊對象,等待新窗口的出現 |
|
Waits for window transitions that would typically take longer than the usual default timeouts. |
||
boolean |
點擊對象的右下角 |
|
Clicks the bottom and right corner of the UI element |
||
boolean |
點擊對象的左上角 |
|
Clicks the top and left corner of the UI element |
||
boolean |
dragTo(UiObject destObj, int steps) |
拖拽到另一個對象處,步長可調整拖拽的速度 |
Drags this object to a destination UiObject. |
||
boolean |
拖拽到目標坐標處,步長可調整拖拽的速度 |
|
Drags this object to arbitrary coordinates. |
||
boolean |
對象是否存在 |
|
Check if view exists. |
||
Rect |
獲取對象的矩形坐標左上角和右下角的坐標 |
|
Returns the view's bounds property. |
||
UiObject |
getChild(UiSelector selector) |
獲得對象的子類對象,可以遞歸獲取 子孫當中的某個對象 |
Creates a new UiObject for a child view that is under the present UiObject. |
||
int |
獲取下一級子類的數量 |
|
Counts the child views immediately under the present UiObject. |
||
String |
獲取類名 |
|
Retrieves the className property of the UI element. |
||
String |
獲取description |
|
Reads the content_desc property of the UI element |
||
UiObject |
getFromParent(UiSelector selector) |
從父類獲取子類,按照UiSelector獲取兄弟類 |
Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject. |
||
String |
獲取包名 |
|
Reads the view's package property |
||
獲得selector用於調試 |
||
Debugging helper. |
||
String |
獲得對象的文本屬性 |
|
Reads the text property of the UI element |
||
Rect |
返回可見視圖的范圍,如果視圖是可見的, 只有可見部分報告的范圍 |
|
Returns the visible bounds of the view. |
||
boolean |
檢查對象的checkable屬性是否為true |
|
Checks if the UI element's checkable property is currently true. |
||
boolean |
檢查對象的checked屬性是否為true |
|
Check if the UI element's checked property is currently true |
||
boolean |
檢查對象的clickable屬性是否為true |
|
Checks if the UI element's clickable property is currently true. |
||
boolean |
檢查對象的enabled屬性是否為true |
|
Checks if the UI element's enabled property is currently true. |
||
boolean |
檢查對象的focusable屬性是否為true |
|
Check if the UI element's focusable property is currently true. |
||
boolean |
檢查對象的focused屬性是否為true |
|
Check if the UI element's focused property is currently true |
||
boolean |
檢查對象的longclickable屬性是否為true |
|
Check if the view's long-clickable property is currently true |
||
boolean |
檢查對象的scrollable屬性是否為true |
|
Check if the view's scrollable property is currently true |
||
boolean |
檢查對象的selected屬性是否為true |
|
Checks if the UI element's selected property is currently true. |
||
boolean |
長按 |
|
Long clicks the center of the visible bounds of the UI element |
||
boolean |
長按對象的右下角 |
|
Long clicks bottom and right corner of the UI element |
||
boolean |
長按對象的左上角 |
|
Long clicks on the top and left corner of the UI element |
||
boolean |
執行單手觸控手勢,可定義任意手勢和形狀 |
|
Performs a multi-touch gesture. |
||
boolean |
執行任意的雙手觸控手勢 |
|
Generates a two-pointer gesture with arbitrary starting and ending points. |
||
boolean |
手勢操作,兩點向內收縮 |
|
Performs a two-pointer gesture, where each pointer moves diagonally toward the other, from the edges to the center of this UiObject . |
||
boolean |
手勢操作,兩點向外張開 |
|
Performs a two-pointer gesture, where each pointer moves diagonally opposite across the other, from the center out towards the edges of the this UiObject. |
||
boolean |
在對象中輸入文字 |
|
Sets the text in an editable field, after clearing the field's content. |
||
boolean |
向下滑動 |
|
Performs the swipe down action on the UiObject. |
||
boolean |
向左滑動 |
|
Performs the swipe left action on the UiObject. |
||
boolean |
向右滑動 |
|
Performs the swipe right action on the UiObject. |
||
boolean |
向上滑動 |
|
Performs the swipe up action on the UiObject. |
||
boolean |
等待對象出現 |
|
Waits a specified length of time for a view to become visible. |
||
boolean |
等待對象消失 |
|
Waits a specified length of time for a view to become undetectable. |
4 UiScrollable
Public Constructors |
||
|
UiScrollable(UiSelector container) |
UiScrollable的構造方法 |
Constructor. |
||
Public Methods |
||
boolean |
以步長為5的值向后滑動 |
|
Performs a backwards fling action with the default number of fling steps (5). |
||
boolean |
以步長為5的值向前滑動 |
|
Performs a forward fling with the default number of fling steps (5). |
||
boolean |
滑動到最前面一屏 |
|
Performs a fling gesture to reach the beginning of a scrollable layout element. |
||
boolean |
滑動到最后一屏 |
|
Performs a fling gesture to reach the end of a scrollable layout element. |
||
getChildByDescription(UiSelector childPattern, String text, boolean allowScrollSearch) |
是否允許滑動來查找具備描述條件的object對象 |
|
Searches for a child element in the present scrollable container. |
||
getChildByDescription(UiSelector childPattern, String text) |
默認滑動來查找具備描述條件的對象 |
|
Searches for a child element in the present scrollable container. |
||
getChildByInstance(UiSelector childPattern, int instance) |
獲取具備UiSelector的子集,再從子集當中篩選想要的元素(不滾動) |
|
Searches for a child element in the present scrollable container that matches the selector you provided. |
||
getChildByText(UiSelector childPattern, String text, boolean allowScrollSearch) |
是否允許滑動來查找具備通過文本條件的object對象 |
|
Searches for a child element in the present scrollable container. |
||
getChildByText(UiSelector childPattern, String text) |
默認通過滑動來查找具備通過文本條件的object對象 |
|
Searches for a child element in the present scrollable container. |
||
int |
獲取搜索滑動過程中的最大滑動次數,默認常量為30 |
|
Gets the maximum number of scrolls allowed when performing a scroll action in search of a child element. |
||
double |
默認常量為0.1,10% |
|
Returns the percentage of a widget's size that's considered as a no-touch zone when swiping. |
||
boolean |
自定義步長向后滑動 |
|
Performs a backward scroll. |
||
boolean |
以默認步長55向后滑動 |
|
Performs a backward scroll with the default number of scroll steps (55). |
||
boolean |
滾動到描述所在的位置 |
|
Performs a forward scroll action on the scrollable layout element until the content-description is found, or until swipe attempts have been exhausted. |
||
boolean |
以默認步長55向前滑動 |
|
Performs a forward scroll with the default number of scroll steps (55). |
||
boolean |
自定義步長向前滑動 |
|
Performs a forward scroll. |
||
boolean |
scrollIntoView(UiSelector selector) |
滾動到元素所在的位置 |
Perform a scroll forward action to move through the scrollable layout element until a visible item that matches the selector is found. |
||
boolean |
scrollIntoView(UiObject obj) |
滾動到對象所在的位置 |
boolean |
滾動到文本對象所在的位置 |
|
Performs a forward scroll action on the scrollable layout element until the text you provided is visible, or until swipe attempts have been exhausted. |
||
boolean |
自定義掃動距離滾動到開始位置 |
|
Scrolls to the beginning of a scrollable layout element. |
||
boolean |
自定義掃動距離以及步長滾動到開始位置 |
|
Scrolls to the beginning of a scrollable layout element. |
||
boolean |
自定義掃動距離以及步長滾動到結束位置 |
|
Scrolls to the end of a scrollable layout element. |
||
boolean |
自定義掃動距離滾動到結束位置 |
|
Scrolls to the end of a scrollable layout element. |
||
UiScrollable |
設置界面為水平滑動 |
|
Set the direction of swipes to be horizontal when performing scroll actions. |
||
UiScrollable |
設置界面為豎直滑動,此為默認值 |
|
Set the direction of swipes to be vertical when performing scroll actions. |
||
UiScrollable |
設置最大可掃描次數 |
|
Sets the maximum number of scrolls allowed when performing a scroll action in search of a child element. |
||
UiScrollable |
設置一個部件的大小,在滑動時,視為無接觸區的百分比 |
|
Sets the percentage of a widget's size that's considered as no-touch zone when swiping. |
5 UiCollection
UiCollection繼承UiObject,用於枚舉一個容器的用戶界面(UI)元素計數的目的,或安裝子元素的文本或描述條件獲取子元素對象。
Public Constructors |
||
|
UiCollection(UiSelector selector) |
UiCollection的構造方法 |
Public Methods |
||
getChildByDescription(UiSelector childPattern, String text) |
通過包含的條件尋找符合的子元素 |
|
Searches for child UI element within the constraints of this UiSelector selector. |
||
getChildByInstance(UiSelector childPattern, int instance) |
通過包含的條件尋找符合的子元素 |
|
Searches for child UI element within the constraints of this UiSelector. |
||
getChildByText(UiSelector childPattern, String text) |
通過包含的條件尋找符合的子元素 |
|
Searches for child UI element within the constraints of this UiSelector. |
||
int |
getChildCount(UiSelector childPattern) |
遞歸計算符合條件的子元素的數量 |
Counts child UI element instances matching the childPattern argument. |
6 UiWatcher
Public Methods |
||
abstract boolean |
抽象方法,在監聽程序中要實現此方法 |
|
監聽器要在中斷代碼之前運行,我們首先要注冊一個監聽器,然后運行。注冊監聽器的方法,在UiDevice的API中有介紹,
void |
registerWatcher(String name, UiWatcher watcher) |
注冊一個監聽器,當前指定動作被打斷的 時候,處理中斷異常 |
Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. |
注冊監聽器的結構方法如下:
UiDevice.getInstance().registerWatcher(“register watcher method”, new UiWatcher(){
Public boolean checkForCondition{
//在此方法體內定義監聽的方法
}
});
編寫用例的注意事項
1.每一個類都要繼承UiAutomatorTestCase
2.最好在每個類里都定義setUp和tearDown方法,用於初始化(最好都以Home界面為測試開始點)和結束測試(最好也以Home界面為測試結束點)
3.因為此套測試框架式繼承與Junit3的,所以每個方法最好都要以test開頭,運行單個類時,程序會run以test開頭的方法。當然這不是強制的,也可以以#方法run用例。