UiAutomator -- UiObject2 API

本文详细介绍了一种UI自动化交互的方法,包括点击与长按、拖拽与滑动等基本操作;输入与清除文本;获取对象属性;属性判断;模拟手势操作;判断对象是否存在;查找对象;以及等待等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.点击与长按

voidclick()

Clicks on this object.

voidclick(long duration)

Performs a click on this object that lasts for duration milliseconds.

<R> RclickAndWait(EventCondition<R> condition, long timeout)

Clicks on this object, and waits for the given condition to become true.

voidlongClick()

Performs a long click on this object.

2.拖拽与滑动

voiddrag(Point dest)

Drags this object to the specified location.

voiddrag(Point dest, int speed)

Drags this object to the specified location.

voidswipe(Direction direction, float percent, int speed)

Performs a swipe gesture on this object.

voidswipe(Direction direction, float percent)

Performs a swipe gesture on this object.

3.输入文本与清除文本

voidsetText(String text)

Sets the text content if this object is an editable field.

voidclear()

Clears the text content if this object is an editable field.

如clear()不起作用,可采取

//将光标移动到文字末尾,按backspace

//将光标移动到文本开头,按del

 

4.获取对象属性

StringgetApplicationPackage()

Returns the package name of the app that this object belongs to.

intgetChildCount()

Returns the number of child elements directly under this object.

List<UiObject2>getChildren()

Returns a collection of the child elements directly under this object.

StringgetClassName()

Returns the class name of the underlying View represented by this object.

StringgetContentDescription()

Returns the content description for this object.

UiObject2getParent()

Returns this object's parent.

StringgetResourceName()

Returns the fully qualified resource name for this object's id.

StringgetText()

Returns the text value for this object.

RectgetVisibleBounds()

Returns the visible bounds of this object in screen coordinates.

PointgetVisibleCenter()

Returns a point in the center of the visible bounds of this object.

5.属性判断:

booleanisCheckable()

Returns whether this object is checkable.

booleanisChecked()

Returns whether this object is checked.

booleanisClickable()

Returns whether this object is clickable.

booleanisEnabled()

Returns whether this object is enabled.

booleanisFocusable()

Returns whether this object is focusable.

booleanisFocused()

Returns whether this object is focused.

booleanisLongClickable()

Returns whether this object is long clickable.

booleanisScrollable()

Returns whether this object is scrollable.

booleanisSelected()

Returns whether this object is selected.

6.模拟手势操作

双指缩放

voidpinchClose(float percent)

Performs a pinch close gesture on this object.

voidpinchClose(float percent, int speed)

Performs a pinch close gesture on this object.

voidpinchOpen(float percent)

Performs a pinch open gesture on this object.

voidpinchOpen(float percent, int speed)

Performs a pinch open gesture on this object.

其他

booleanfling(Direction direction, int speed)

Performs a fling gesture on this object.

booleanfling(Direction direction)

Performs a fling gesture on this object.

booleanscroll(Direction direction, float percent, int speed)

Performs a scroll gesture on this object.

booleanscroll(Direction direction, float percent)

Performs a scroll gesture on this object.

voidsetGestureMargin(int margin)

Sets the margins used for gestures in pixels.

voidsetGestureMargins(int left, int top, int right, int bottom)

Sets the margins used for gestures in pixels.

7.判断对象是否存在

booleanhasObject(BySelector selector)

Returns whether there is a match for the given criteria under this object.

8.获取该对象

UiObject2findObject(BySelector selector)

Searches all elements under this object and returns the first object to match the criteria.

List<UiObject2>findObjects(BySelector selector)

Searches all elements under this object and returns all objects that match the criteria.

9.等待

<R> Rwait(UiObject2Condition<R> condition, long timeout)

Waits for given the condition to be met.

<R> Rwait(SearchCondition<R> condition, long timeout)

Waits for given the condition to be met.

转载于:https://www.cnblogs.com/biang/p/6403570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值