airtest之脚本批量运行

Posted xuanjian-91

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了airtest之脚本批量运行相关的知识,希望对你有一定的参考价值。

项目目录

  技术图片

  根目录:D:workspaceairtestsuite

  脚本:douyin.air,dy.air

  日志目录:log

    日志目录由custom_launcher.py自动生成,custom_launcher.py目录:D:workspaceairtestsuitecustom_launcher.py,即在根目录下

       summary.html:自动生成的

       summary_template.html:复制进去即可

        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="file:///D:/workspace/airtest/suite/log/{{r.name}}/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>

   custom_launcher.py目录:D:workspaceairtestsuitecustom_launcher.py,即在根目录下

 custom_launcher.py直接执行即可,如下

技术图片

 

   custom_launcher.py内容:

     

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()
    #roor_dir是脚本根目录
    def run_air(self, root_dir=‘D:\\workspace\\airtest\\suite‘, device=[‘android://127.0.0.1:5037/2476a88e‘]):
        # 聚合结果
        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"): # f为.air案例名称:douyin.air airName = f script = os.path.join(root_dir, f) # script路径(它是脚本路径):D:workspaceairtestsuitedouyin.air print(script) # 日志存放路径和名称:D:workspaceairtestsuitelogdouyin log = os.path.join(root_dir, ‘log‘ + ‘\\‘ + airName.replace(‘.air‘, ‘‘)) print(log) #判断目录在不在,在,则删除目录中的内容,否则,新建每个脚本log目录 if os.path.isdir(log): shutil.rmtree(log) else: os.makedirs(log) print(str(log) + ‘is created‘) #日志的输出文件log:D:workspaceairtestsuitelogdouyinlog.html output_file = log + ‘\\‘ + ‘log.html‘ #命令行参数解析后,参数的数据格式Namespace(device=device, log=log, recording=None, script=script) #python -m airtest run D:workspaceairtestdy.air --device Android://127.0.0.1:5037/B2T5T16C06000021 --log "D:workspaceairtestdouyin.air2" args = Namespace(device=device, log=log, recording=None, script=script)
#自定义启动的核心代码 try: run_script(args, AirtestCase) except: pass finally: rpt = report.LogToHtml(script, log) #测试结果模板渲染,"log_template.html"时airtest自带的模板,output_file日志存放路径 rpt.report("log_template.html", output_file=output_file) #结果保存在result对象中 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 ) #summary_template.html相对路径,跟我配置一样即可 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/2476a88e‘]#自定义 test.run_air(‘D:\\workspace\\airtest\\suite‘, device)#自定义

  

  

  




以上是关于airtest之脚本批量运行的主要内容,如果未能解决你的问题,请参考以下文章

Airtest之web自动化——在本地环境运行airtest脚本

airtest 多airtest脚本批量执行

Airtest 执行脚本(批量)

教你如何批量运行自动化脚本,高效工作!

Airtest API精讲之设备连接管理API集合

Airtest引用其他的 .air 脚本