点击
poco("btn_start").click()
poco.click([0.14,0.2])
poco('star_single').long_click()
获取属性
name1 = poco("star_single").get_name()
name2 = poco("star_single").attr('name')
text = poco("star_single").get_text()
for name in poco("playLocalPositioning").child("fish").offspring("name"):
print(name.get_text())
不需要输入完毕后自动按回车键
text("test", enter=False)
自动回车失败
text("11")
ime=G.DEVICE.yosemite_ime
ime.code(6)
输入框点击输入键盘上的 搜索
按钮
text("test", search=True)
判断控件是否存在
if x.exists():
设置属性
poco("pos_input").set_text("123")
poco("pos_input").setattr('text',"456")
匹配控件的name属性
poco(nameMatches="com.*?songInfo")
poco(nameMatches="Subscribe to.*").click()
for i in poco(nameMatches="com.*?songInfo"):
print(i.get_text())
匹配控件的text属性
poco(textMatches=".*淘宝")
poco(nameMatches=".*portalTitle",textMatches=".*推荐")
匹配控件的type属性
poco(typeMatches=".*TextView")
节点的属性及预期的属性值来进行定位
poco(name="淘宝")
元素之间的渲染层级关系进行选择,父子关系、兄弟关系等
poco("plays").child("playBasic")
元素索引顺序,逐个选中单个元素
poco("Content").child(type="Text")[0]
手指按照顺序依次滑过多个坐标
device().swipe_along([[959, 418],[1157, 564],[1044, 824],[751, 638],[945, 415]])
向内捏合 device().pinch(in_or_out='in', center=None, percent=0.5) sleep(1.0)
向外捏合 device().pinch(in_or_out='out', center=None, percent=0.2) sleep(1.0)
双指滑动 device().two_finger_swipe( (200, 900), (700, 900),duration=0.8, steps=5, offset=(0, 80))
打开应用start_app("com.netease.newsreader.activity")
等待超时
poco().wait_for_all(self, objects, timeout=120)
poco().wait_for_any(self, objects, timeout=120)#在超时时长结束之前,等待任意一个UI显示出来,即一次轮询任何一个给定的UI
bomb = poco("bomb")
yellow = poco("yellow")
blue = poco("blue")
while True:
fish = poco.wait_for_any([bomb,yellow,blue])
print(fish.get_name())
poco().wait_stable()
poco().long_click(self, pos, duration=2.0)
poco().scroll(self, direction='vertical', percent=0.6, duration=2.0)#给定的时间间隔内按屏幕,然后释放。第一个参数是坐标位置,第二个参数是长按的时间
poco().snapshot(self, width=720)#屏幕滚动,第一个参数滚动方向:水平\垂直,第二个参数是根据方向滚动距离占整个屏幕高度或宽度的百分比,第三个参数是执行操作的时间间隔
install(filepath='xxx/xxx.apk') # 安装应用
uninstall(package)#卸载app,参数是app的包名
wake()#唤醒连接的手机
home()#设置连接的手机返回home界面
等待页面上某1个元素出现或者消失,等待的超时时间 timeout
默认为120秒,如果在超时时长之内元素没有出现或者消失的话,会报 PocoTargetTimeout
的错误
poco("bomb").wait_for_appearance()
poco("bomb").wait_for_disappearance()
左上角(0,0),右下角(1,1),横坐标为x,纵坐标为y
点击节点的某个位置
node.focus('center').click() // 点击节点的中心点位置
node.focus([0.1, 0.1]).long_click() // 点击节点的靠近左上角位置
node.focus([1, 1]).long_click() // 点击节点的右下角位置
node_right_edge = node.focus([1, 0.5])
拖拽
从一个节点位置拖拽到另一个节点位置
poco('ANodeName').drag_to(poco('BNodeName'))
从列表的一端滑动到另一端
listView = poco('Scroll View')
listView.focus([0.5, 0.8]).drag_to(listView.focus([0.5, 0.2]))
滑动列表
#滑动列表
poco('Scroll View').swipe([0, -0.1])
poco('Scroll View').swipe('up')
#从A点滑动到B点
x, y = poco('Scroll View').get_position()
end = [x, y - 0.1]
poco.swipe([x, y], end)
#从A点向指定方向和定长移动
x, y = poco('Scroll View').get_position()
dir = [0, -0.1]
poco.swipe([x, y], direction=dir)
异常处理
try:
node.click() #对找不到的节点执行了操作或者获取属性
except PocoNoSuchNodeException:
print('oops!')
try:
poco.click([1.1, 1.1]) # 选中和操作的UI在屏幕外
except InvalidOperationException:
print('【error】tips')
node= poco('node')
try:
star.wait_for_appearance(timeout=3) # 某个定时器结束时触发,但如果操作太快,节点还没显示在界面上
except PocoTargetTimeout:
print('oops!')
PocoTargetRemovedException #某个节点已经被删除时会触发
PocoTargetTimeout
某个定时器结束时触发,但如果操作太快,节点还没显示在界面上,很可能是触发的PocoNoSuchNodeException
node= poco('node')
try:
star.wait_for_appearance(timeout=3) # wait until appearance within 3s
except PocoTargetTimeout:
print('oops!')
获取输入法的 ID
device().yosemite_ime._get_ime_list()
切换某个输入法
adb shell ime enable 输入法ID #启动
adb shell ime set 输入法ID #设置
查看当前使用的输入法
adb shell settings get secure default_input_method
用于执行 ADB 指令的 shell
接口:shell("ime set 输入法ID")
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
from airtest.core.api import *
auto_setup(__file__)
ym = "com.netease.nie.yosemite/.ime.ImeService"
sh = "com.sohu.inputmethod.sogouoem/.SogouIME"
def set_ime(ime):
shell("ime enable " + ime)
shell("ime set " + ime)
set_ime(ym)
text("123", enter=False)
set_ime(sh)
参考官方文档:AirtestProject,Poco自动化测试脚本官方API学习笔记 - 知乎