选择器
选择器是一种在当前窗口中标识特定UI对象的便捷机制
d(text=“1”,className=“2”)
- 选择器支持以下参数
text,textContains,textMatches,textStartsWith
className, classNameMatches
description,descriptionContains,descriptionMatches,descriptionStartsWith
checkable,checked,clickable,longClickable
scrollable,enabled,focusable,focused,selected
packageName, packageNameMatches
resourceId, resourceIdMatches
index, instance - Children and siblings(孩子和兄弟姐妹)
- children
得到孩子或孙子ui对象
d.(classname=“1”).child(text=“2”) - siblings
得到兄弟姐妹UI对象
d.(text=“1”).sibling(classname=“2”) - 通过文字、描述或者实例得到子ui对象
- child_by_text
- 让子ui对象匹配classname=“1"和带有文本"b”
d(className=“1”, resourceId=“2”) .child_by_text(“b”, className=“3”) - 通过允许滚动来获取子项
d(className=“1”, resourceId=“2”).child_by_text(“b”,allow_scroll_search=True,className=“3”)
allow_scroll_search=True#允许滚动搜索
- 让子ui对象匹配classname=“1"和带有文本"b”
- child_by_description是找到孙子有指定描述的孩子,其他参数类似child_by_text
- child_by_instance是在子层次结构中的任何位置找到具有子UI元素的子元素,该元素位于指定的实例中。它在可见视图上执行而无需滚动。
支持链式调用
d(className=“1”, resourceId=“2”) .child_by_text(“Wi‑Fi”, className=“3”) \ .child(className=“4”) \ .click()
- child_by_text
- children
- 相对定位
- 可以通过相对定位获取UI对象
d(A).left(B)#选择a左侧的b
d(A).right(B)#选择a右侧的b
d(A).up(B)#选择a上面的b
d(A).down(B)#选择a下面的b - d(text=“Wi‑Fi”).right(className=“1”).click()
- 可以通过相对定位获取UI对象
- 多个实例
- 有时屏幕可能包含多个具有相同属性的视图,例如text,需要使用选择器中的“instance”属性来选择一个合格的实例
d(text=“A”, instance=0)#带有text="A"的第一个UI对象 - 另外uiautomotor2提供了类似列表的api
d(text=“A”).count#获取当前屏幕上带有text="A"的视图计数
d(text=“Add new”)[0]#通过索引取
d(text=“Add new”)[1]
注意:在遍历结果列表的代码块中使用选择器时,必须确保屏幕上的UI元素保持不变。否则,当迭代列表时可能发生Element-Not-Found错误
- 有时屏幕可能包含多个具有相同属性的视图,例如text,需要使用选择器中的“instance”属性来选择一个合格的实例
Watcher(观察者)
- 当选择器找不到匹配项时,可以注册观察者用于执行某些操作
- 注册watcher
- 条件匹配时点击
d.watcher(“命令”).when(text=“1”).when(text=“2”).click(text=“3”)
d.watcher()#创建一个新的watcher
.when()#观察者的触发条件
.click()#对目标执行单击操作 - 条件成立时按键
d.watcher(“AUTO_FC_WHEN_ANR”).when(text=“ANR”).when(text=“Wait”) .press(“back”, “home”)
.press(“back”, “home”)#按照顺序依次按键
- 条件匹配时点击
- 检查命名的观察者是否被触发
- 触发观察者,这意味着观察者已经运行且所有条件都匹配
d.watcher(“watcher_name”).triggered
返回布尔值
- 触发观察者,这意味着观察者已经运行且所有条件都匹配
- 删除已命名的观察者
d.watcher(“watcher_name”).remove() - 列出所有的观察者
d.watchers
返回列表 - 检查是否有任何触发的观察者
d.watchers.triggered - 重置所有触发的观察者
d.watchers.reset() - 删除观察者
d.watchers.remove()
d.watchers.remove(“watchers_name”)#删除单个 - 强制运行所有的观察者
d.watchers.run() - 页面更新时运行所有观察者
通常可以用来自动点击权限确认框,或者自动安装
d.watcher(“OK”).when(text=“OK”).click(text=“OK”)
d.watchers.watched = True#启动自动触发器
d.watchers.watched = False#禁用自动触发器
d.watchers.watched == False#获取当前触发watcher状态
全局设置
- 设置延迟1.5秒后ui点击
d.click_post_delay = 1.5#默认没有延迟 - 设置默认元素等待超时
d.wait_timeout = 30.0 - UiAutomator中的超时设置(隐藏方法)
d.jsonrpc.getConfigurator()
d.jsonrpc.setConfigurator()
为了防止客户端程序响应超时,waitForIdleTimeout和waitForSelectorTimeout目前已改为0
输入法
通常用用于不知道控件的情况下输入,第一步需要切换输入法,然后发送adb广播命令
- 实例
d.set_fastinput_ime(True)#切换成FastInputIME输入法
d.send_keys(“你好123abcEFG”)# adb广播输入
d.clear_text()#清除输入框所有内容(需要android-uiautomator.apk版本大于1.0.7 )
d.set_fastinput_ime(False)#切换成正常的输入法
d.send_action(“search”)#模拟输入法的搜索
参数go search send next done previous
toast(提示信息)
- 显示toast
d.toast.show(“你好”)
d.toast.show(“你好”,1.0)#显示1秒 - 得到toast
d.toast.get_message(5.0, 10.0, “default message”)- 第一个参数:最大等待超时
- 第二个参数:缓存时间
- 第三个参数:默认消息
d.toast.reset()
xpath
https://github.com/openatx/uiautomator2#xpath
xpath是指通过路径定位
- 使用方法
self.d.xpath(//*).click() - 详细用法
- 所有元素
//* - resource-id包含login字符
//*[contains(@resource-id, ‘login’)] - 按钮包含账号1或帐号2
//android.widget.Button[contains(@text, ‘账号1’) or contains(@text, ‘帐号2’)] - 所有ImageView中的第二个
(//android.widget.ImageView)[2] - 所有ImageView中的最后一个
(//android.widget.ImageView)[last()] - className包含ImageView
//*[contains(name(), “ImageView”)]
- 所有元素