Android自动化测试基础知识——UiAutomator--4.3以上(跨应用UI测试)

目录

使用UI Automator Viewer 进行元素定位

UiAutomator API

UiDevice

Public methods

Uiselector

UiObject

Public methods

 UiCollection

UiScrollable

使用


使用UI Automator Viewer 进行元素定位

 

UiAutomator API

UI Automator 测试框架提供了一个 UiDevice 类,用于在运行目标应用的设备上访问和执行操作。您可以调用其方法来访问设备属性,如当前屏幕方向或显示屏尺寸。UiDevice 类还可让您执行如下操作:

  • 改变设备的旋转。
  • 按硬件键,如“音量调高按钮”。
  • 按返回、主屏幕或菜单按钮。
  • 打开通知栏。
  • 截取当前窗口的屏幕截图。

例如,要模拟按主屏幕按钮的操作,请调用 UiDevice.pressHome() 方法。

来源: https://developer.android.google.cn/training/testing/ui-automator

 

  • UiCollection:枚举容器的界面元素,目的是为了计数,或者按可见文本或内容说明属性来定位子元素。
  • UiObject:表示设备上可见的界面元素。
  • UiScrollable:支持搜索可滚动界面容器中的项目。
  • UiSelector:表示对设备上的一个或多个目标界面元素的查询。
  • Configurator:可让您设置用于运行 UI Automator 测试的关键参数。

UiDevice

来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiDevice

Public methods

voidclearLastTraversedText()

Clears the text from the last UI traversal event.

booleanclick(int x, int y)

Perform a click at arbitrary coordinates specified by the user

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

Performs a swipe from one coordinate to another coordinate.

voiddumpWindowHierarchy(File dest)

Dump the current window hierarchy to a File.

voiddumpWindowHierarchy(OutputStream out)

Dump the current window hierarchy to an OutputStream.

voiddumpWindowHierarchy(String fileName)

This method is deprecated. Use dumpWindowHierarchy(File) or dumpWindowHierarchy(OutputStream) instead.

UiObjectfindObject(UiSelector selector)

Returns a UiObject which represents a view that matches the specified selector criteria.

UiObject2findObject(BySelector selector)

Returns the first object to match the selector criteria, or null if no matching objects are found.

List<UiObject2>findObjects(BySelector selector)

Returns all objects that match the selector criteria.

voidfreezeRotation()

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

StringgetCurrentActivityName()

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

StringgetCurrentPackageName()

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

 

Uiselector

来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector.html

UiSelectorpackageName(String name)

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

UiSelectorpackageNameMatches(String regex)

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

UiSelectorresourceId(String id)

Set the search criteria to match the given resource ID.

UiSelectorresourceIdMatches(String regex)

Set the search criteria to match the resource ID of the widget, using a regular expression.

UiSelectorscrollable(boolean val)

Set the search criteria to match widgets that are scrollable.

UiSelectorselected(boolean val)

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

UiSelectortext(String text)

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

UiSelectortextContains(String 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.

UiSelectortextMatches(String regex)

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

UiSelectortextStartsWith(String text)

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

StringtoString()

UiObject

来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiObject.html

Public methods

voidclearTextField()

Clears the existing text contents in an editable field.

booleanclick()

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

booleanclickAndWaitForNewWindow()

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

booleanclickAndWaitForNewWindow(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.

booleanclickBottomRight()

Clicks the bottom and right corner of the UI element

booleanclickTopLeft()

Clicks the top and left corner of the UI element

booleandragTo(UiObject destObj, int steps)

Drags this object to a destination UiObject.

booleandragTo(int destX, int destY, int steps)

Drags this object to arbitrary coordinates.

booleanexists()

Check if view exists.

RectgetBounds()

Returns the view's bounds property.

UiObjectgetChild(UiSelector selector)

Creates a new UiObject for a child view that is under the present UiObject.

intgetChildCount()

Counts the child views immediately under the present UiObject.

StringgetClassName()

Retrieves the className property of the UI element.

StringgetContentDescription()

Reads the content_desc property of the UI element

UiObjectgetFromParent(UiSelector selector)

Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject.

StringgetPackageName()

Reads the view's package property

final UiSelectorgetSelector()

Debugging helper.

StringgetText()

Reads the text property of the UI element

 UiCollection

booleanclickTopLeft()

Clicks the top and left corner of the UI element

booleandragTo(UiObject destObj, int steps)

Drags this object to a destination UiObject.

booleandragTo(int destX, int destY, int steps)

Drags this object to arbitrary coordinates.

booleanexists()

Check if view exists.

RectgetBounds()

Returns the view's bounds property.

UiObjectgetChild(UiSelector selector)

Creates a new UiObject for a child view that is under the present UiObject.

intgetChildCount()

Counts the child views immediately under the present UiObject.

StringgetClassName()

Retrieves the className property of the UI element.

StringgetContentDescription()

Reads the content_desc property of the UI element

UiObjectgetFromParent(UiSelector selector)

Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject.

StringgetPackageName()

Reads the view's package property

final UiSelectorgetSelector()

Debugging helper.

StringgetText()

Reads the text property of the UI element

 

UiScrollable

 

来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiScrollable.html

booleanscrollBackward()

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

booleanscrollDescriptionIntoView(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.

booleanscrollForward(int steps)

Performs a forward scroll.

booleanscrollForward()

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

booleanscrollIntoView(UiObject obj)

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

booleanscrollIntoView(UiSelector selector)

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

booleanscrollTextIntoView(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.

booleanscrollToBeginning(int maxSwipes)

Scrolls to the beginning of a scrollable layout element.

booleanscrollToBeginning(int maxSwipes, int steps)

Scrolls to the beginning of a scrollable layout element.

booleanscrollToEnd(int maxSwipes)

Scrolls to the end of a scrollable layout element.

booleanscrollToEnd(int maxSwipes, int steps)

Scrolls to the end of a scrollable layout element.

UiScrollablesetAsHorizontalList()

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

UiScrollablesetAsVerticalList()

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

使用

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值