UiAutomator自動化測試框架介紹html
環境搭建java
1 必要條件node
1.1 JDKandroid
1.2 SDK(API高於15)正則表達式
1.3 Eclipseshell
2 簡要步驟express
2.1 安裝JDK並添加bin目錄到環境變量架構
2.2 添加SDK中的tools目錄和platform-tools到環境變量app
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用例。