一、元素定位
-
建议尽量使用 text 定位元素
# 定位一个元素
poco(text='选择门店')
# 如果text匹配多个元素,获取多个元素
ele_list=list(poco(text='选择门店').wait(5))
# 模糊定位,支持正则
poco(textMatches="'^门店.*$'")
2. 如果不能使用text定位,常用局部定位
# 子元素
poco(name='com.tencent.mm:id/nb').child(text='选择门店')
# 后代
poco(name='com.tencent.mm:id/nb').offspring(text='选择门店')
# 父
poco(name='com.tencent.mm:id/nb').parent()
# 所有子元素
poco(name='com.tencent.mm:id/nb').children()
# 兄弟元素
poco(name='com.tencent.mm:id/nb').sibling(text='选择门店')
# 同样resourceid的元素列表
list(poco(name='com.tencent.mm:id/nb'))
二、元素定位
-
通过相对坐标,控制点击的具体位置。左上角 (0, 0),右下角 (1, 1),横坐标为 x,纵坐标为 y
po = poco(text='main_node')
# 点击节点的中心点位置, 默认点击中心位置
po.focus('center').click()
# 点击节点的靠近左上角位置
po.focus([0.1, 0.1]).click()
# 点击节点的右下角位置
po.focus([1, 1]).click()
2. 等待元素的出现或者消失
实际写用例时,有一些扫描或缓冲场景,需要等待元素出现或消失,才能进行下一步操作
# 当使用wait_for_appearance或wait_for_disappearance时,建议处理PocoTargetTimeout,并截图,以方便在报告中查看出错时的页面情况
try:
poco(name='com.tencent.mm:id/nb').wait_for_appearance(timeout=10)
poco(name='com.tencent.mm:id/nb').wait_for_disappearance(timeout=10)
except PocoTargetTimeout:
snapshot(msg="元素出现或未出现")
- 滑动和拖动