轉自:https://blog.csdn.net/u010127154/article/details/83375659#commentsedit
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 這個模板放在同目錄下即可
像這樣 :