Airtest搭建批量执行UI自动化脚本,并批量生成html 报告
admin
2023-07-05 20:43:39
0

  Airtest 是近几年出的,帮助文档很少,反复查看源帮助文档和其他相关资料,编写了一个安卓UI自动化的轻量小框架。

  使用语言:python3.6.8 开发工具:pycharm; python安装airtest :

  pip install airtest

  框架结构如下:

  1、框架简单介绍

  air 是多个自动化脚本

  conf,lib 配置文件

  data 数据

  log 日志

  report html 报告

  XFDAirtestCase.py 入口方法,启动器

  2、air 脚本的开发

  通过airtest工具,开发脚本,可以录制,也可以自己编写,请参考源文档,这里不做多介绍

  ·http://airtest.netease.com/docs/cn/index.html

  3、配置文件,获取文件所在目录

  # -*- coding: utf-8 -*

  import os

  Androiddevice=["Android://127.0.0.1:5037/172.16.81.115:5555"]#连接安卓设备127.0.0.1:5037固定写法172.16.81.115安卓真机的Ip

  airpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'air')#脚本目录

  logpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'log')#日志目录

  templatepath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'templates')#模板目录

  reportpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'report')#报告目录

  datapath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data')#测试数据目录

  4、入口脚本,启动器

  from airtest.cli.runner import AirtestCase, run_script

  from argparse import *

  import shutil

  from conf.config import *

  from lib.log import logger

  from airtest.report.report import simple_report

  class XFDAirtestCase(AirtestCase):#继承AirtestCase类

  def setUp(self):

  logger.info("案例开始执行")

  super(XFDAirtestCase, self).setUp()#继承父类的setup方法

  def tearDown(self):

  logger.info("案例执行结束")

  super(XFDAirtestCase, self).tearDown()#继承父类的tearDown方法

  def run_air(self, root_dir, device):#本方法主要是查找脚本文件,目录文件,初始化AirtestCase所需要的参数,执行脚本,并生成报告

  for f in os.listdir(root_dir):#循环查找air所在的目录

  if f.endswith(".air"):#以air结尾的文件

  airName = f

  script = os.path.join(root_dir, f)#脚本目录

  logger.info(script)

  log = os.path.join(logpath +'\\'+ airName.replace('.air', ''))#日志目录

  logger.info(log)

  if os.path.isdir(log):

  shutil.rmtree(log)#清空日志目录文件

  else:

  os.makedirs(log)

  args = Namespace(device=device, log=log, recording=None, script=script)#初始化父类AirtestCase所需要的参数

  try:

  run_script(args, AirtestCase)#执行air脚本文件

  except AssertionError:

  logger.info("案例执行失败")

  finally:

  #output_file = log + '\\' + airName.replace('.air', '') + '.html'

  output_file = reportpath + '\\' + airName.replace('.air', '') + '.html'#生成报告目录

  simple_report(script, logpath=log, output=output_file)#生成报告的方法

  logger.info("案例执行成功")

  if __name__ == '__main__':

  test = XFDAirtestCase()

  #device = ['Android://127.0.0.1:5037/172.16.81.115:5555']

  test.run_air(airpath, Androiddevice)

  5、执行过程,以及执行结果报告

  在python环境下执行入口脚本,命令如下:python XFDAirtestCase.py 循环执行air 文件下的air 脚本,以登录为例,每个air脚本对应生成一个html报告, 放到report 目录中;html 中有详细的点击步骤,也有截图,报告效果如下:

  6、搭建框架中遇到的问题

  (1)airtest adb 与python中的adb 冲突问题,

  解决方案:把\tools\adb39 中的adb 拷贝到 pyhon 的\Lib\site-packages\airtest\core\android\static\adb\windows 目录下

  (2) 入口脚本 运行问题,

  直接运行XFDAirtestCase.py 报错:错误如下:

  Error

  Traceback (most recent call last):

  File "D:\python36\lib\site-packages\airtest\cli\runner.py", line 26, in setUpClass

  cls.args = args

  NameError: name 'args' is not defined

  解决: 通过python 命令直接执行 进到该项目下 直接执行

  python XFDAirtestCase.py


相关内容

热门资讯

我国科学家为细胞信号“导航”开... 新华社济南5月31日电(记者张力元)人体细胞犹如一座精密的通信城市,每天都有大量“指令”穿梭传递,调...
极端大风突袭哈尔滨!过山车停摆... 极目新闻记者 詹钘5月31日,受强对流天气影响,哈尔滨国际会展中心体育场相关设施受到损坏,原计划当晚...
三原电缆取得电缆接头连接用防护... 国家知识产权局信息显示,上海三原电缆附件有限公司取得一项名为“一种电缆接头连接用防护结构”的专利,授...
原创 识... 还是那句话,机圈苦大屏久已…… 虽然大屏有大屏的美,但是小屏也有小屏的俏。在大屏旗舰占据主流的手机市...
玄戒技术取得分频电路专利,实现... 国家知识产权局信息显示,北京玄戒技术有限公司取得一项名为“分频电路、分频器、射频芯片和电子设备”的专...
为什么今年香会基调明显变了 5月29日—31日在新加坡举行的第23届香格里拉对话会(简称“香会”),见证着元首引领下大国关系继续...
成本几毛钱、假驱蚊液香精兑水,... 入夏升温,蚊虫进入活跃期,驱蚊防护成为民生刚需,《财经调查》持续接到消费者投诉,他们买到的多款网红驱...
越来越多80后90后,正在丧失... 六一儿童节到来之际,朋友圈里开始出现一种熟悉的热闹。有人晒出零食礼包,有人半开玩笑地向伴侣讨礼物,还...
洋保电子取得用于低温环境的电气... 国家知识产权局信息显示,洋保电子(太仓)有限公司取得一项名为“一种用于低温环境的电气柜”的专利,授权...
中日韩飞手争霸宁波!2026无... 潮新闻客户端 记者 陈冲 通讯员 朱凝 5月31日,2026小遛·无人机竞速世界杯(中国·宁波鄞州站...