airtest 批量执行脚本
1.先创建好一个目录,像这样
2.sj.air 是录制好的air脚本
里面的内容像这样:
3.最好保持air脚本和.py名字一致
4.在该目录下创建summary_template.html
<!DOCTYPE html>
<html>
<head>
<title>测试结果汇总</title>
<style>
.fail {
color: red;
width: 7emem;
text-align: center;
}
.success {
color: green;
width: 7emem;
text-align: center;
}
.details-col-elapsed {
width: 7em;
text-align: center;
}
.details-col-msg {
width: 7em;
text-align: center;
background-color:#ccc;
}
</style>
</head>
<body>
<div>
<div><h2>Test Statistics</h2></div>
<table width="800" border="thin" cellspacing="0" cellpadding="0">
<tr width="600">
<th width="300" class='details-col-msg'>案例名称</th>
<th class='details-col-msg'>执行结果</th>
</tr>
{% for r in results %}
<tr width="600">
<td class='details-col-elapsed'><a href="log.html" target="view_window">{{r.name}}</a></td>
<td class="{{'success' if r.result else 'fail'}}">{{"成功" if r.result else "失败"}}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
5.创建执行脚本 名字如my.py
#encoding='unicode_escape'
#coding=utf-8
# from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from airtest.cli.runner import AirtestCase, run_script
from argparse import *
import airtest.report.report as report
import jinja2
import shutil
import os
import io
class CustomAirtestCase(AirtestCase):
def setUp(self):
print("custom setup")
# add var/function/class/.. to globals
# self.scope["hunter"] = "i am hunter"
# self.scope["add"] = lambda x: x+1
# exec setup script
# self.exec_other_script("setup.owl")
super(CustomAirtestCase, self).setUp()
def tearDown(self):
print("custom tearDown")
# exec teardown script
# self.exec_other_script("teardown.owl")
super(CustomAirtestCase, self).setUp()
def run_air(self, root_dir='C:\\Users\\hui\\Desktop\\project', device=['android://127.0.0.1:5037/5HR623695']):
# 聚合结果
results = []
# 获取所有用例集
root_log = root_dir + '\\' + 'log'
if os.path.isdir(root_log):
shutil.rmtree(root_log)
else:
os.makedirs(root_log)
print(str(root_log) + 'is created')
for f in os.listdir(root_dir):
if f.endswith(".air"):
airName = f
script = os.path.join(root_dir, f)
print(script)
log = os.path.join(root_dir, 'log' + '\\' + airName.replace('.air', ''))
print(log)
if os.path.isdir(log):
shutil.rmtree(log)
else:
os.makedirs(log)
print(str(log) + 'is created')
output_file = log + '\\' + 'log.html'
args = Namespace(device=device, log=log, recording=None, script=script)
try:
run_script(args, AirtestCase)
except:
pass
finally:
rpt = report.LogToHtml(script, log)
rpt.report("log_template.html", output_file=output_file)
result = {}
result["name"] = airName.replace('.air', '')
result["result"] = rpt.test_result
results.append(result)
# 生成聚合报告
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(root_dir),
extensions=(),
autoescape=True
)
template = env.get_template("summary_template.html", root_dir)
html = template.render({"results": results})
output_file = os.path.join(root_dir, "summary.html")
with io.open(output_file, 'w', encoding="utf-8") as f:
f.write(html)
print(output_file)
if __name__ == '__main__':
test = CustomAirtestCase()
device = ['android://127.0.0.1:5037/5HR6R6023695'] #5HR6R1023695 10.247.4.182
test.run_air('C:\\Users\\hui\\Desktop\\project', device)
这里面有几个要注意的地方:
1.device = ['android://127.0.0.1:5037/5HR6R19316023695'] #5HR616023695 10.247.4.1
android://127.0.0.1:5037 为固定模式;后面的内容为adb devices 获取的内容,不是设备的ip地址,这也是导致执行的时候提示 xxx目录下没有log.txt的原因
2.在确认1满足的情况下,还要 手动安装pip3 install pocoui,也就不会报UnicodeDecodeError: 'utf-8',虽然这个问题提示的有点怪,然后执行即可安装我们录制的内容在端内进行回放,同时在目录下生成log,log下有日志和html报告,像这样 :
对了 summary_template.html 这个模板放在同目录下即可
像这样 :