您身边的网站建设顾问,影视剪辑,云优化软件,化妆培训网站模板App爬虫之强大的Airtest的操作总结
App爬虫之强大的Airtest的操作总结
# Python使用该框架需要安装的依赖库
pip install airtest
pip install poco
pip install pocouifrom airtest.core.api import *
from airtest.cli.parser import cli_setup
from poco.drivers.android.…App爬虫之强大的Airtest的操作总结
App爬虫之强大的Airtest的操作总结
# Python使用该框架需要安装的依赖库
pip install airtest
pip install poco
pip install pocoui
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
自动配置运行环境 如果没有连接设备 默认连接安卓设备
参数basedir - 设置当前脚本所在路径也可以直接传_file_变量进来devices - 一个内容为connect_device url 字符串的列表logdir - 可设置脚本运行是log保存路径默认值为None则不保存log如果设置为True则自动保存在basedir/log目录中project_root - 用于设置PROJECT_ROOT变量方便using接口调用if not cli_setup():auto_setup(__file__, logdirTrue, devices[android://127.0.0.1:5037/10.2.145.168:5555?cap_methodMINICAPori_methodMINICAPORItouch_methodMAXTOUCH])# 连接本机默认端口连的设备号为123和456的两台手机
# auto_setup(__file__,devices[Android://127.0.0.1:5037/123,Androd://127.0.0.1:5037/456])poco AndroidUiautomationPoco(use_airtest_inputTrue, screenshot_each_actionFalse)# script content
print(start...)# 唤醒并解锁目标设备
wake()# 启动APP
start_app(com.bonade.xxp)# home键操作
# home()# 若是设置的APP开着则关闭APP
# stop_app(com.bonade.xxp)# 安装待测软件apk路径信息。
# install(path/to/your/apk)# 卸载安装
# uninstall(package_name_of_your_apk)一、 定位方式
child获取当前节点下的子节点 如果是多个获取多个用for循环获取不使用循环提取的默认提取第一个节点
def child(self, nameNone, **attrs): 示例
menuList1poco(com.aa.bb:ide_bottom_navigator).child(android.widget.LinearLayout)
for i in range(0,len(menuList1)): #同列表方式一致节点序号从0开始print (第 %d 次用child获取menuList底部全部菜单%s%(i,menuList1[i]))children获取子节点 获取字节点如果是多个获取多个用for循环获取不使用循环提取的默认提取第一个节点
def children(self): 示例
menuList2poco(com.tencent.nbagametime:ide_bottom_navigator).child(android.widget.LinearLayout).children()
for i in range(0,len(menuList2)):print (第 %d 次用children获取menuList底部菜单名%s%(i,menuList2[i]))offspring孙节点 获取当前节点下的孙节点如果是多个获取多个用for循环获取不使用循环提取的默认提取第一个节点
def offspring(self, nameNone, **attrs): offspring查找孙节点的顺序树结构自底往上同一层从中间右边左边的顺序 sibling兄弟节点 获取当前节点的兄弟节点
def sibling(self, nameNone, **attrs): parent父节点 获取当前节点的父节点如果获取的当前节点是多个用children获取的则获取列表第一个节点的父节点
def parent(self): 注意此方法在Android上可用在Ios上不可用会报错AttributeError: ‘UIObjectProxy’ object has no attribute ‘parent’ attr(‘type’)提取属性值 示例
poco(ssion).attr(type) #提取指定元素属性为type的值get_text()提取文本内容 示例
poco(ssion).get_text() #提取指定元素的文本内容exists()判断元素是否存在 示例
poco(ssion).exists() #判断指定元素是否在当前屏幕上存在返回True元素文本正则匹配
poco(textMatches^测试.*$, typeButton, enableTrue)二、 元素操作
click()点击操作
def click(self, focusNone, sleep_intervalNone): focus值为(x,y)或“anchor”或“center”。(x,y)意思是距元素左上角的偏移点值必须在0~1范围内。 center是指点击ui元素边界框的中心。 anchor”是指监视器中UI包围盒的小红点。 sleep_interval点击操作后等待的秒数。默认值为无这里默认睡眠间隔。这个值可以通过POCO初始化进行配置。 示例
poco(home_bottom_navigator).click() #无参数默认点中间位置无间隔时间
poco(home_bottom_navigator).click([0.5, 0.5]) #相当于点中间位置
poco(home_bottom_navigator).click([0.5, 0.5],3) #传参点击元素中间红点后等待3秒rclick()右键点击
def rclick(self, focusNone, sleep_intervalNone): focus同click方法 sleep_interval同click方法
double_click()双击操作
def double_click(self, focusNone, sleep_intervalNone): focus同click方法 sleep_interval同click方法
long_click()长按操作
def long_click(self, duration2.0): duration整个动作持续时间
swipe()滑动操作
def swipe(self, direction, focusNone, duration0.5): direction坐标可以是(x,y)格式坐标也可以是’up’, down’, left’, right’ (up[0, -0.1]down[0, 0.1]left[-0.1, 0]right[0, 0.1]) focus同click方法 duration间隔时间,float类型默认0.5秒 示例
node poco(home_bottom_navigator).child(point_img)
node.swipe(up) #向上滑动
node.swipe([0.2, -0.2]) # 以45度角向上和向右滑动sqrt(0.08)单位距离说明swipe操作以 锚点anchor为起点如果想改变起点可用 focus 方法然后朝给定方向滑动距离就是向量的长度。
drag_to拖拽
def drag_to(self, target, duration2.0): target拖动后的目标元素 duration间隔时间,float类型默认2秒 备注
区别 darg 是从一个UI拖到另一个UI 而 swipe 是将一个UI朝某个方向拖动。 示例 .
poco(text比赛).drag_to(poco(text开始)) #把比赛元素拖动到开始元素上scroll卷动效果
就是从一个方向一点一点的显示出来 一种特效就是下拉页面啊或者其他移动的时候眼睛会比较舒适其实区别不大。
def scroll(self, directionvertical, percent0.6, duration2.0): direction滚动方向。”垂直“或水平”默认垂直 percent在指定元素上按高或宽的滚动百分比 duration间隔时间,float类型默认2秒
pinch捏合操作 双指捏合是一个比较常见的手势操作了我们经常打开相册时用这个手势去放大、缩小图片以便查看。
def pinch(self, directionin, percent0.6, duration2.0, dead_zone0.1): direction滚动方向。”垂直“或水平”默认垂直 percent在指定元素上按高或宽的滚动百分比 duration间隔时间,float类型默认2秒 dead_zone
focus局部定位
def focus(self, f): f同click()方法 示例
poco(比赛).focus(center).click() #定位到元素中间点进行点击
poco(比赛).focus([0.5,1]).click() #定位到元素最下边缘的中间点进行点击将 focus 和 drag_to 结合使用还能产生卷动(scroll)的效果下面例子展示了如何将一个列表向上卷动半页。 第9点中的scroll实现方式就是这样的 示例
poco(“元素”).focus([0.5, 0.8]).drag_to(poco(“元素”).focus([0.5, 0.2])) #从指定节点的中下方拖拽到中上方比如手机端的从下往上滑动 start_gesture()移动设备手势分解方法, 返回用于生成序列化手势操作的对象。 示例
ui1poco(ui1)
ui2poco(ui2)
ui1.start_gesture().hold(1).to(ui2).hold(1).up() #在ui1元素上按下等待1秒拖拽到ui2元素上再等1秒最后抬起释放get_position()获取元素位置坐标 返回(x, y)坐标
def get_position(self, focusNone) focus同click()方法默认空 wait()等待直到超过timeout的时间超时后返回元素对象 def wait(self, timeout3) 说明 在给定时间内等待一个UI出现并返回这个元素如果已经存在了那就返回这个元素。如果超时还没出现同样也会返回但是调用这个UI的操作时会报错。
经验结合exists()方法可以判断元素是否出现比如 poco(“元素”).wait(4).exists() #如果结果是True则元素存在如果False则元素不存在
wait_for_appearance():等待出现若超时返回
PocoTargetTimeout def wait_for_appearance(self, timeout120) 备注 wait_for_appearance()同wait()的区别前者超时没找到元素直接返回异常PocoTargetTimeout后者超时后还返回元素当调用时会报错。 wait_for_disappearance()等待未出现 若超时返回
PocoTargetTimeout def wait_for_disappearance(self, timeout120): attr()获取属性 def attr(self, name) name属性名 示例
poco(节点).attr(text)说明通过给定的属性名检索ui元素的属性。如果属性不存在则返回none。 属性名可以是以下类型之一也可以是由SDK实现的任何其他自定义类型 - - visible: whether or not it is visible to user
- text: string value of the UI element
- type: the type name of UI element from remote runtime
- pos: the position of the UI element
- size: the percentage size [width, height] in range of 0~1 according to the screen
- name: the name of UI element
- ...: other sdk implemented attributessetattr()设置属性值 更改ui元素的属性值。
def setattr(self, name, val) name属性名 val属性值 说明并非所有属性都可以转换为文本。如果改变不可变的属性或不存在的属性将引发InvalidOperationException异常 exists()判断是否存在 返回True/False
def exists(self) get_text()获取UI元素的文本属性 如果没有此类属性则返回None
def get_text(self) set_text()设置文本值
def set_text(self, text) text文本值
get_name()获取元素名 即属性是name的值 attr(name’)
def get_name(self) get_size()获取元素size 即属性中的size(attr(size’)) 值size : [1, 0.76328125]
def get_size(self): get_bounds()获取UI元素边界框的参数
def get_bounds(self) 说明返回列表形式如 (top, right, bottom, left)形式的与标准坐标系中屏幕边缘相关的坐标。
nodes在远程运行时中访问UI元素的只读属性。 def nodes(self) 调用方式
poco(节点).nodesinvalidate():重新获取元素标识
def invalidate(self) 说明清除标志以指示重新查询或从层次结构中重新选择UI元素 等待时间强制等待\全局隐式等待\显示等待时间 全局隐式等待
ST.FIND_TIMEOUT60 #设置隐式等待60秒隐式等待 是一种智能等待他可以规定在查找元素时在指定时间内不断查找元素
如果找到则代码继续执行直到超时没找到元素才会报错也就是说如果在第三秒找到元素则剩下的7秒不会被等待 显式等待wait() 1、显示等待也是一种智能等待在指定超时时间范围内只要满足操作的条件就会继续执行后续代码 如果不满足条件则会一直等到超时 强制等待sleep() 隐式等待和显示等待的区别 1、隐式等待是全局的可以随时更改的显示等待是针对单一元素或者一组元素的 2、隐式等待只能针对元素查找方法显示等待可以自定义等待条件