程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Airtest搭建批量执行UI自动化脚本,并批量生成html 报告

发布于2019-08-15 11:47     阅读(1249)     评论(0)     点赞(5)     收藏(2)


版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tianxiexingyun/article/details/90606510

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

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

 pip install airtest

框架结构如下:

1、框架简单介绍

  1. air 是多个自动化脚本
  2. conf,lib 配置文件
  3. data 数据
  4. log 日志
  5. report html 报告
  6. XFDAirtestCase.py  入口方法,启动器

2、air 脚本的开发

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

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

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

  1. # -*- coding: utf-8 -*
  2. import os
  3. Androiddevice=["Android://127.0.0.1:5037/172.16.81.115:5555"]#连接安卓设备127.0.0.1:5037固定写法172.16.81.115安卓真机的Ip
  4. airpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'air')#脚本目录
  5. logpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'log')#日志目录
  6. templatepath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'templates')#模板目录
  7. reportpath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'report')#报告目录
  8. datapath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data')#测试数据目录

4、入口脚本,启动器

  1. from airtest.cli.runner import AirtestCase, run_script
  2. from argparse import *
  3. import shutil
  4. from conf.config import *
  5. from lib.log import logger
  6. from airtest.report.report import simple_report
  7. class XFDAirtestCase(AirtestCase):#继承AirtestCase类
  8. def setUp(self):
  9. logger.info("案例开始执行")
  10. super(XFDAirtestCase, self).setUp()#继承父类的setup方法
  11. def tearDown(self):
  12. logger.info("案例执行结束")
  13. super(XFDAirtestCase, self).tearDown()#继承父类的tearDown方法
  14. def run_air(self, root_dir, device):#本方法主要是查找脚本文件,目录文件,初始化AirtestCase所需要的参数,执行脚本,并生成报告
  15. for f in os.listdir(root_dir):#循环查找air所在的目录
  16. if f.endswith(".air"):#以air结尾的文件
  17. airName = f
  18. script = os.path.join(root_dir, f)#脚本目录
  19. logger.info(script)
  20. log = os.path.join(logpath +'\\'+ airName.replace('.air', ''))#日志目录
  21. logger.info(log)
  22. if os.path.isdir(log):
  23. shutil.rmtree(log)#清空日志目录文件
  24. else:
  25. os.makedirs(log)
  26. args = Namespace(device=device, log=log, recording=None, script=script)#初始化父类AirtestCase所需要的参数
  27. try:
  28. run_script(args, AirtestCase)#执行air脚本文件
  29. except AssertionError:
  30. logger.info("案例执行失败")
  31. finally:
  32. #output_file = log + '\\' + airName.replace('.air', '') + '.html'
  33. output_file = reportpath + '\\' + airName.replace('.air', '') + '.html'#生成报告目录
  34. simple_report(script, logpath=log, output=output_file)#生成报告的方法
  35. logger.info("案例执行成功")
  36. if __name__ == '__main__':
  37. test = XFDAirtestCase()
  38. #device = ['Android://127.0.0.1:5037/172.16.81.115:5555']
  39. 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 报错:错误如下:

  1. Error
  2. Traceback (most recent call last):
  3.   File "D:\python36\lib\site-packages\airtest\cli\runner.py", line 26, in setUpClass
  4.     cls.args = args
  5. NameError: name 'args' is not defined

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

python XFDAirtestCase.py

 



所属网站分类: 技术文章 > 博客

作者:378273283782232

链接:https://www.pythonheidong.com/blog/article/35917/aaf14c272fd1dbc9363c/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)