UiAutomator自动化测试框架介绍

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库

UiAutomator自动化测试框架介绍

3         添加Android库

找到sdk/platforms/android-19/下面的android.jar和uiautomator.jar并添加进来

UiAutomator自动化测试框架介绍

4         添加完成后如下图

UiAutomator自动化测试框架介绍

在工程中编写case

1         分析Device当前界面的UI内容

在编写Case之前,需要找到界面上不同控件的id,text,class或者description等等,来定位具体的点击事件。我们可以使用uiautomatorviewer工具来分析界面的组织架构。

UiAutomator自动化测试框架介绍

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可以查看

UiAutomator自动化测试框架介绍

path:
工程目录

2        
build工程jar文件

2.1配置文件创建完成后,刷新工程目录,将会产生三个文件:build.xml、local.properties和project.properties

UiAutomator自动化测试框架介绍

2.2    打开build.xml,将default值改成build,如下图

UiAutomator自动化测试框架介绍

2.3    build工程

UiAutomator自动化测试框架介绍

运行工程

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

clearLastTraversedText()

清除上一次输入的文本

Clears the text
from the last UI traversal event.

boolean

click(int x, int y)

点击坐标

Perform a click
at arbitrary coordinates specified by the user

boolean

drag(int startX, int startY, int endX, int endY, int
steps)

从开始坐标拖拽到终点坐标位置

Performs a swipe
from one coordinate to another coordinate.

void

dumpWindowHierarchy(String fileName)

dump当前界面的层次结构,并保存到/data/local/tmp/目录下

Helper method
used for debugging to dump the current window's layout hierarchy.

void

freezeRotation()

禁用传感器,并且在当前界面冻结转屏

Disables the
sensors and freezes the device rotation at its current rotation state.

String

getCurrentActivityName()

获取当前Activity的名字,此方法已被丢弃,不建议使用

This method is
deprecated. The results returned should be considered unreliable

String

getCurrentPackageName()

获取当前包名

Retrieves the
name of the last package to report accessibility events.

int

getDisplayHeight()

获取显示高度,以像素为单位

Gets the height
of the display, in pixels.

int

getDisplayRotation()

返回当前的旋转值,0,90,180,270

Returns the current rotation of the display, as defined
in Surface

Point

getDisplaySizeDp()

 

Returns the
display size in dp (device-independent pixel) The returned display size is
adjusted per screen rotation.

int

getDisplayWidth()

获取显示宽度,以像素为单位

Gets the width
of the display, in pixels.

static UiDevice

getInstance()

Java单例模式,获取UiDevice的对象

Retrieves a
singleton instance of UiDevice

String

getLastTraversedText()

获取最后一次输入的text

Retrieves the
text from the last UI traversal event received.

String

getProductName()

获取设备的名字

Retrieves the
product name of the device.

boolean

hasAnyWatcherTriggered()

检查是否有监听器触发过

Checks if any registered UiWatcher have
triggered.

boolean

hasWatcherTriggered(String watcherName)

检查某个特定的监听器是否触发过

Checks if a specific registered UiWatcher has
triggered.

boolean

isNaturalOrientation()

检查设备是否是正常的竖屏状态

Check if the
device is in its natural orientation.

boolean

isScreenOn()

检查屏幕是否亮起

Checks the power
manager if the screen is ON.

boolean

openNotification()

打开notification通知栏

Opens the
notification shade.

boolean

openQuickSettings()

打开quicksetting

Opens the Quick
Settings shade.

boolean

pressBack()

按返回键

Simulates a
short press on the BACK button.

boolean

pressDPadCenter()

轨迹球

Simulates a
short press on the CENTER button.

boolean

pressDPadDown()

轨迹球

Simulates a
short press on the DOWN button.

boolean

pressDPadLeft()

轨迹球

Simulates a
short press on the LEFT button.

boolean

pressDPadRight()

轨迹球

Simulates a
short press on the RIGHT button.

boolean

pressDPadUp()

轨迹球

Simulates a
short press on the UP button.

boolean

pressDelete()

删除键

Simulates a
short press on the DELETE key.

boolean

pressEnter()

回车键

Simulates a
short press on the ENTER key.

boolean

pressHome()

home键

Simulates a
short press on the HOME button.

boolean

pressKeyCode(int keyCode, int metaState)

keycode,参照keyevent

Simulates a
short press using a key code.

boolean

pressKeyCode(int keyCode)

keycode,参照keyevent

Simulates a
short press using a key code.

boolean

pressMenu()

menu键

Simulates a
short press on the MENU button.

boolean

pressRecentApps()

recent键

Simulates a
short press on the Recent Apps button.

boolean

pressSearch()

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

removeWatcher(String name)

移除之前注册的监听器

Removes a previously registered UiWatcher.

void

resetWatcherTriggers()

重置一个监听器

Resets a UiWatcher that has been triggered.

void

runWatchers()

强制运行所有的监听器

This method
forces all registered watchers to run.

void

setCompressedLayoutHeirarchy(boolean compressed)

应用或禁用布局层次压缩

Enables or
disables layout hierarchy compression.

void

setOrientationLeft()

通过禁用传感器,然后模拟设备向左转,并且固定位置

Simulates
orienting the device to the left and also freezes rotation by disabling the
sensors.

void

setOrientationNatural()

通过禁用传感器,然后模拟设备转到其自然默认的方向,并且固定位置

Simulates
orienting the device into its natural orientation and also freezes rotation
by disabling the sensors.

void

setOrientationRight()

通过禁用传感器,然后模拟设备向右转,并且固定位置

Simulates
orienting the device to the right and also freezes rotation by disabling the
sensors.

void

sleep()

锁屏

This method
simply presses the power button if the screen is ON else it does nothing if
the screen is already OFF.

boolean

swipe(Point[] segments, int segmentSteps)

在点阵列中滑动,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

takeScreenshot(File storePath)

当前窗口截屏,并将其存储为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

takeScreenshot(File storePath, float scale, int quality)

当前窗口截屏,可以自定义尺寸和图片质量

Take a screenshot
of current window and store it as PNG The screenshot is adjusted per screen
rotation

void

unfreezeRotation()

重新启用传感器,并且允许旋转屏幕

Re-enables the
sensors and un-freezes the device rotation allowing its contents to rotate
with the device physical rotation.

void

waitForIdle(long timeout)

等待当前应用程序处于空闲状态

Waits for the
current application to idle.

void

waitForIdle()

等待当前应用程序处于空闲状态

Waits for the
current application to idle.

boolean

waitForWindowUpdate(String packageName, long timeout)

等待窗口内容更新时间的发生

Waits for a
window content update event to occur.

void

wakeUp()

唤醒屏幕

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

 

UiSelector()

构造方法

Public Methods

UiSelector

checkable(boolean val)

是否是可选择的属性

Set the search criteria to match widgets that are
checkable.

UiSelector

checked(boolean val)

是否已经被选择

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

className(String className)

类名

Set the search criteria to match the class property for
a widget (for example, "android.widget.Button").

<T> UiSelector

className(Class<T> type)

泛型类名

Set the search criteria to match the class property for
a widget (for example, "android.widget.Button").

UiSelector

classNameMatches(String regex)

类名正则表达式

Set the search criteria to match the class property for
a widget, using a regular expression.

UiSelector

clickable(boolean val)

是否可以被点击

Set the search criteria to match widgets that are
clickable.

UiSelector

description(String desc)

通过content-description来选取

Set the search criteria to match the
content-description property for a widget.

UiSelector

descriptionContains(String desc)

通过content-description所包含的字符来选取

Set the search criteria to match the
content-description property for a widget.

UiSelector

descriptionMatches(String regex)

通过content-description所符合正则表达式

的字符来选取

Set the search criteria to match the
content-description property for a widget.

UiSelector

descriptionStartsWith(String desc)

通过content-description开始字符匹配来选取

Set the search criteria to match the
content-description property for a widget.

UiSelector

enabled(boolean val)

是否已经被enabled

Set the search criteria to match widgets that are
enabled.

UiSelector

focusable(boolean val)

焦点属性

Set the search criteria to match widgets that are
focusable.

UiSelector

focused(boolean val)

当前焦点属性

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

index(int index)

编号

Set the search criteria to match the widget by its node
index in the layout hierarchy.

UiSelector

instance(int instance)

索引

Set the search criteria to match the widget by its
instance number.

UiSelector

longClickable(boolean val)

是否可以被长按

Set the search criteria to match widgets that are
long-clickable.

UiSelector

packageName(String name)

包名

Set the search criteria to match the package name of
the application that contains the widget.

UiSelector

packageNameMatches(String regex)

正则包名

Set the search criteria to match the package name of
the application that contains the widget.

UiSelector

resourceId(String id)

资源id

Set the search criteria to match the given resource ID.

UiSelector

resourceIdMatches(String regex)

资源id正则表达式

Set the search criteria to match the resource ID of the
widget, using a regular expression.http://blog.bettersoftwaretesting.com/

UiSelector

scrollable(boolean val)

是否可以被滑动

Set the search criteria to match widgets that are
scrollable.

UiSelector

selected(boolean val)

当前选择属性

Set the search criteria to match widgets that are
currently selected.

UiSelector

text(String text)

根据text来选取控件或者应用程序

Set the search criteria to match the visible text
displayed in a widget (for example, the text label to launch an app).

UiSelector

textContains(String text)

根据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

textMatches(String regex)

通过正则表达式来选取匹配的控件或者应用程序

Set the search criteria to match the visible text
displayed in a layout element, using a regular expression.

UiSelector

textStartsWith(String text)

通过文本开始字符来选取

Set the search criteria to match visible text in a
widget that is prefixed by the text parameter.

String

toString()

 

Protected Methods

UiSelector

cloneSelector()

克隆UiSelector

3        
UiObject

UiObject主要对对象进行操作

UiAutomator自动化测试框架介绍

UiAutomator自动化测试框架介绍

Public Constructors

 

UiObject(UiSelector selector)

UiObject的构造方法

Constructs a UiObject to represent a
view that matches the specified selector criteria.

Public Methods

void

clearTextField()

清除编辑框中的文本

Clears the existing text contents in an
editable field.

boolean

click()

点击事件

Performs a click at the center of the
visible bounds of the UI element represented by this UiObject.

boolean

clickAndWaitForNewWindow(long timeout)

点击对象,等待新窗口的出现,

参数为等待超时时长

Performs a click at the center of the
visible bounds of the UI element represented by this UiObject and waits for
window transitions.

boolean

clickAndWaitForNewWindow()

点击对象,等待新窗口的出现

Waits for window transitions that would
typically take longer than the usual default timeouts.

boolean

clickBottomRight()

点击对象的右下角

Clicks the bottom and right corner of
the UI element

boolean

clickTopLeft()

点击对象的左上角

Clicks the top and left corner of the UI
element

boolean

dragTo(UiObject destObj, int steps)

拖拽到另一个对象处,步长可调整拖拽的速度

Drags this object to a destination
UiObject.

boolean

dragTo(int destX, int destY, int steps)

拖拽到目标坐标处,步长可调整拖拽的速度

Drags this object to arbitrary coordinates.

boolean

exists()

对象是否存在

Check if view exists.

Rect

getBounds()

获取对象的矩形坐标左上角和右下角的坐标

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

getChildCount()

获取下一级子类的数量

Counts the child views immediately under
the present UiObject.

String

getClassName()

获取类名

Retrieves
the className property of the UI element.

String

getContentDescription()

获取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

getPackageName()

获取包名

Reads the
view's package property

final UiSelector

getSelector()

获得selector用于调试

Debugging helper.

String

getText()

获得对象的文本属性

Reads the text property of the
UI element

Rect

getVisibleBounds()

返回可见视图的范围,如果视图是可见的,

只有可见部分报告的范围

Returns the visible bounds of the view.

boolean

isCheckable()

检查对象的checkable属性是否为true

Checks if the UI
element's checkable property is currently true.

boolean

isChecked()

检查对象的checked属性是否为true

Check if the UI
element's checked property is currently true

boolean

isClickable()

检查对象的clickable属性是否为true

Checks if the UI
element's clickable property is currently true.

boolean

isEnabled()

检查对象的enabled属性是否为true

Checks if the UI
element's enabled property is currently true.

boolean

isFocusable()

检查对象的focusable属性是否为true

Check if the UI
element's focusable property is currently true.

boolean

isFocused()

检查对象的focused属性是否为true

Check if the UI
element's focused property is currently true

boolean

isLongClickable()

检查对象的longclickable属性是否为true

Check if the
view's long-clickable property is currently true

boolean

isScrollable()

检查对象的scrollable属性是否为true

Check if the
view's scrollable property is currently true

boolean

isSelected()

检查对象的selected属性是否为true

Checks if the UI
element's selected property is currently true.

boolean

longClick()

长按

Long clicks the center of the visible
bounds of the UI element

boolean

longClickBottomRight()

长按对象的右下角

Long clicks bottom and right corner of
the UI element

boolean

longClickTopLeft()

长按对象的左上角

Long clicks on the top and left corner
of the UI element

boolean

performMultiPointerGesture(PointerCoords... touches)

执行单手触控手势,可定义任意手势和形状

Performs a multi-touch gesture.

boolean

performTwoPointerGesture(Point startPoint1, Point
startPoint2, Point endPoint1, Point endPoint2, int steps)

执行任意的双手触控手势

Generates a two-pointer gesture with
arbitrary starting and ending points.

boolean

pinchIn(int percent, int steps)

手势操作,两点向内收缩

Performs a two-pointer gesture, where
each pointer moves diagonally toward the other, from the edges to the center
of this UiObject .

boolean

pinchOut(int percent, int steps)

手势操作,两点向外张开

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

setText(String text)

在对象中输入文字

Sets the text in an editable field,
after clearing the field's content.

boolean

swipeDown(int steps)

向下滑动

Performs the swipe down action on the
UiObject.

boolean

swipeLeft(int steps)

向左滑动

Performs the swipe left action on the
UiObject.

boolean

swipeRight(int steps)

向右滑动

Performs the swipe right action on the
UiObject.

boolean

swipeUp(int steps)

向上滑动

Performs the swipe up action on the
UiObject.

boolean

waitForExists(long timeout)

等待对象出现

Waits a specified length of time for a
view to become visible.

boolean

waitUntilGone(long timeout)

等待对象消失

Waits a specified length of time for a
view to become undetectable.

4        
UiScrollable

UiAutomator自动化测试框架介绍

UiAutomator自动化测试框架介绍

Public Constructors

 

UiScrollable(UiSelector container)

UiScrollable的构造方法

Constructor.

Public Methods

boolean

flingBackward()

以步长为5的值向后滑动

Performs a backwards fling action with
the default number of fling steps (5).

boolean

flingForward()

以步长为5的值向前滑动

Performs a forward fling with the
default number of fling steps (5).

boolean

flingToBeginning(int maxSwipes)

滑动到最前面一屏

Performs a fling gesture to reach the
beginning of a scrollable layout element.

boolean

flingToEnd(int maxSwipes)

滑动到最后一屏

Performs a fling gesture to reach the
end of a scrollable layout element.

UiObject

getChildByDescription(UiSelector childPattern,
String text, boolean allowScrollSearch)

是否允许滑动来查找具备描述条件的object对象

Searches for a child element in the
present scrollable container.

UiObject

getChildByDescription(UiSelector childPattern,
String text)

默认滑动来查找具备描述条件的对象

Searches for a child element in the
present scrollable container.

UiObject

getChildByInstance(UiSelector childPattern,
int instance)

获取具备UiSelector的子集,再从子集当中筛选想要的元素(不滚动)

Searches for a child element in the
present scrollable container that matches the selector you provided.

UiObject

getChildByText(UiSelector childPattern,
String text, boolean allowScrollSearch)

是否允许滑动来查找具备通过文本条件的object对象

Searches for a child element in the
present scrollable container.

UiObject

getChildByText(UiSelector childPattern,
String text)

默认通过滑动来查找具备通过文本条件的object对象

Searches for a child element in the
present scrollable container.

int

getMaxSearchSwipes()

获取搜索滑动过程中的最大滑动次数,默认常量为30

Gets the maximum number of scrolls
allowed when performing a scroll action in search of a child element.

double

getSwipeDeadZonePercentage()

默认常量为0.1,10%

Returns the percentage of a widget's
size that's considered as a no-touch zone when swiping.

boolean

scrollBackward(int steps)

自定义步长向后滑动

Performs a backward scroll.

boolean

scrollBackward()

以默认步长55向后滑动

Performs a backward scroll with the
default number of scroll steps (55).

boolean

scrollDescriptionIntoView(String text)

滚动到描述所在的位置

Performs a forward scroll action on the
scrollable layout element until the content-description is found, or until
swipe attempts have been exhausted.

boolean

scrollForward()

以默认步长55向前滑动

Performs a forward scroll with the
default number of scroll steps (55).

boolean

scrollForward(int steps)

自定义步长向前滑动

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)

滚动到对象所在的位置

Perform a forward scroll action to move through the
scrollable layout element until a visible item that matches
the UiObject is found.

boolean

scrollTextIntoView(String text)

滚动到文本对象所在的位置

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

scrollToBeginning(int maxSwipes)

自定义扫动距离滚动到开始位置

Scrolls to the beginning of a scrollable
layout element.

boolean

scrollToBeginning(int maxSwipes, int steps)

自定义扫动距离以及步长滚动到开始位置

Scrolls to the beginning of a scrollable
layout element.

boolean

scrollToEnd(int maxSwipes, int steps)

自定义扫动距离以及步长滚动到结束位置

Scrolls to the end of a scrollable
layout element.

boolean

scrollToEnd(int maxSwipes)

自定义扫动距离滚动到结束位置

Scrolls to the end of a scrollable
layout element.

UiScrollable

setAsHorizontalList()

设置界面为水平滑动

Set the direction of swipes to be
horizontal when performing scroll actions.

UiScrollable

setAsVerticalList()

设置界面为竖直滑动,此为默认值

Set the direction of swipes to be
vertical when performing scroll actions.

UiScrollable

setMaxSearchSwipes(int swipes)

设置最大可扫描次数

Sets the maximum number of scrolls
allowed when performing a scroll action in search of a child element.

UiScrollable

setSwipeDeadZonePercentage(double swipeDeadZonePercentage)

设置一个部件的大小,在滑动时,视为无接触区的百分比

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

UiObject

getChildByDescription(UiSelector childPattern, String text)

通过包含的条件寻找符合的子元素

Searches for child UI element within the constraints of
this UiSelector selector.

UiObject

getChildByInstance(UiSelector childPattern, int instance)

通过包含的条件寻找符合的子元素

Searches for child UI element within the constraints of
this UiSelector.

UiObject

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

UiAutomator自动化测试框架介绍

Public Methods

abstract boolean

checkForCondition()

抽象方法,在监听程序中要实现此方法

The testing framework calls this handler method automatically when the framework is unable to find a match using the UiSelector.

监听器要在中断代码之前运行,我们首先要注册一个监听器,然后运行。注册监听器的方法,在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

UiAutomator自动化测试框架介绍

2.最好在每个类里都定义setUp和tearDown方法,用于初始化(最好都以Home界面为测试开始点)和结束测试(最好也以Home界面为测试结束点)

UiAutomator自动化测试框架介绍

3.因为此套测试框架式继承与Junit3的,所以每个方法最好都要以test开头,运行单个类时,程序会run以test开头的方法。当然这不是强制的,也可以以#方法run用例。

上一篇:Spring定时器,定时执行(quartz)


下一篇:ms sql 2005和2008收缩日志的方法