Airtest遍历脚本执行

# -*- coding: utf-8 -*
import os
from airtest.cli.runner import AirtestCase, run_script
import airtest.report.report as report
from argparse import *
import shutil
#import jinja2
#from conf.config import *
#from lib.log import lib_logger
import logging
from airtest.report.report import simple_report

Androiddevice = ["Android://127.0.0.1:5037/HKL48N6K"]  # 连接安卓设备127.0.0.1:5037固定写法172.16.81.115安卓真机的Ip
airpath = os.path.join(os.path.dirname('F:\\test'),'test')  # 脚本目录
#airpath = os.path.join(os.path.dirname('C:\\Users\\Administrator\\AppData\\Local\\Temp\\AirtestIDE\\scripts'),'scripts')
logpath = os.path.join(os.path.dirname('F:\\test\\log'), 'log')  # 日志目录
templatepath = os.path.join(os.path.dirname('F:\\test\\templates'),'templates')  # 模板目录
reportpath = os.path.join(os.path.dirname('F:\\test\\report'), 'report')  # 报告目录
datapath = os.path.join(os.path.dirname('F:\\test\\data'), 'data')  # 测试数据目录
logging.getLogger().setLevel(logging.INFO)


class XFDAirtestCase(AirtestCase):  # 继承AirtestCase类

    def setUp(self):
        logging.info("案例开始执行")
        super(XFDAirtestCase, self).setUp()  # 继承父类的setup方法

    def tearDown(self):
        logging.info("案例执行结束")
        super(XFDAirtestCase, self).tearDown()  # 继承父类的tearDown方法

    def run_air(self, root_dir, device):  # 本方法主要是查找脚本文件,目录文件,初始化AirtestCase所需要的参数,执行脚本,并生成报告
        #results = []
        for f in os.listdir(root_dir):  # 循环查找air所在的目录
            if f.endswith(".air"):  # 以air结尾的文件
                airName = f
                script = os.path.join(root_dir, f)  # 脚本目录
                logging.info(script)
                log = os.path.join(logpath + '\\' + airName.replace('.air', ''))  # 日志目录
                logging.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:
                    logging.info("案例执行失败")
                finally:
                    # output_file = log + '\\' + airName.replace('.air', '') + '.html'
                    output_file = reportpath + '\\' + airName.replace('.air', '') + '.html'  # 生成报告目录
                    simple_report(script, logpath=log, output=output_file)  # 生成报告的方法
                    logging.info("案例执行成功")
                    #eoutput_file = logpath + '\\' + aiName.replace('.air', '') + '.html'
                    #print(output_file+'111')
                    #print("script:"+script)
                    #output_file = log + '\\' + 'log.html'
                    # 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(airpath),
        #     extensions=(),
        #     autoescape=True
        # )
        # template = env.get_template("summary_template.html", airpath)
        # html = template.render({"results": results})
        # output_file = os.path.join(airpath, "summary.html")
        # with io.open(output_file, 'w', encoding="utf-8") as f:
        #     f.write(html)
        # print(output_file)





if __name__ == '__main__':
    test = XFDAirtestCase()
    # device = ['Android://127.0.0.1:5037/172.16.81.115:5555']
    test.run_air(airpath, Androiddevice)

你可能感兴趣的:(Airtest遍历脚本执行)