发布于2019-08-15 11:47 阅读(1249) 评论(0) 点赞(5) 收藏(2)
Airtest 是近几年出的,帮助文档很少,反复查看源帮助文档和其他相关资料,编写了一个安卓UI自动化的轻量小框架。
使用语言:python3.6.8 开发工具:pycharm; python安装airtest :
pip install airtest
框架结构如下:
通过airtest工具,开发脚本,可以录制,也可以自己编写,请参考源文档,这里不做多介绍
·http://airtest.netease.com/docs/cn/index.html
- # -*- 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')#测试数据目录
- 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)
在python环境下执行入口脚本,命令如下:python XFDAirtestCase.py 循环执行air 文件下的air 脚本,以登录为例,每个air脚本对应生成一个html报告, 放到report 目录中;html 中有详细的点击步骤,也有截图,报告效果如下:
(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
链接:https://www.pythonheidong.com/blog/article/35917/aaf14c272fd1dbc9363c/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!