拆解: | 1.检测crash | 在脚本遇到异常时检查app进程是否活动 |
| 2.保存log | 如果进程不是活跃状态 保存系统logcat日志文件(也可以游戏自身的日志文件) |
| 3.报告处理 | 修改Airtext.report渲染函数及模版文件。当日志目录下出现logcat日志时生成网页附带额外的信息 |
具体方式1:(不改airtest源码)
脚本头部:清理logcat旧日志,并删除之前残留的文件
脚本中部:使用try except 捕获异常
脚本尾部:捕获到异常后判断进程是否还存在,如果不存在表示异常退出,同时保存额外的log
# init
a=device()
appname=包名
a.shell('logcat -c')
logcat_path=os.path.join(G.LOGGER.logfile,"..\\logcat.txt")
if os.path.exists(logcat_path):
os.remove(logcat_path)
try:
…… # do sth.
except:
try:
str='ps|grep '+appname
a.shell (str)
except:
with open(logcat_path,'wb') as f:
for x in a.logcat(grep_str=appname):
f.write(x)
示例:
——————————————————————————————————————————————
报告处理:
以下需要修改环境变量中python第三方库中的airtest源码文件注意备份(也可以用独立的虚拟环境):
位置一般为C:\Users\用户名\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\airtest\
1.修改html模版添加所需的内容 .report\log_template.html
{% if logcat_path %}
<a href={{logcat_path}}>
<label class="red" lang="en" href=>[crash_log]</label>
</a>
{% endif %}
示例:
2.修改report模块渲染函数 .report\report.py
logcat_path = os.path.join(self.logfile,"..\\logcat.txt")
if os.path.exists(logcat_path):
data['logcat_path'] = logcat_path
示例:
3.修改语言文件zh_CN.js .report\js\langpack\zh_CN.js
新增一行 "[crash_log]": "app出现异常停止,点击查看系统日志",
——————————————————————————————————————————————————
创建快捷方式
将下面内容保存未run_test.ps1放在脚本air文件夹内右键使用Powershell运行:
$x = Split-Path -Parent $MyInvocation.MyCommand.Definition
python -m airtest run $x --device Android:/// --log $x
python -m airtest report $x --log_root $x --outfile "$x\log.html" --lang zh
实际效果:脚本运行过程中出席程序异常停止的情况会在【运行失败】之后额外标注