• Airtest启动器的妙用--添加自定义的命令行参数


    前言

    我们都知道,在命令行运行Airtest脚本时, airtest run test.air 指令后面附带的参数是固定的:

    • --device ,用来指定连接的被测设备
    • --log ,用来指定log内容和截图存放的目录
    • --recording ,运行脚本时进行录屏操作

    如果想要在运行脚本的命令中,添加一些自定义的参数,我们就要使用到Airtest的启动器啦。

    Airtest启动器的介绍

    启动器实际上也是一个 .py 文件,里面所编写的内容,实际上是基于1个叫 AirtestCase 的类,这个类继承了 unittest.TestCase ,其目的在于Airtest在运行用例脚本时,添加所有执行基础Airtest脚本的相关功能。

    因此,假如我们需要添加自定的义功能,就只需要在 AirtestCase 类的基础上,往 setup()teardown() 方法中加入自己的代码即可。如果这些设置和功能内容相对固定,可以将这些内容作为一个启动器(launcher),用来在运行实际测试用例之前初始化相关的自定义环境。

    from airtest.cli.runner import AirtestCase, run_script
    from airtest.cli.parser import runner_parser
    
    
    class CustomAirtestCase(AirtestCase):
    
        def setUp(self):
            pass
            super(CustomAirtestCase, self).setUp()
    
        def tearDown(self):
            pass
            super(CustomAirtestCase, self).tearDown()
    
    
    if __name__ == '__main__':
        ap = runner_parser()
        args = ap.parse_args()
        run_script(args, CustomAirtestCase)
    

    在启动器中添加自定义的命令行参数

    了解了启动器的基本含义之后,我们再来看下,如何利用启动器来帮助我们给 airtest run 指令添加自定义的命令行参数。

    假设我们需要往命令行传入1个参数,用于指定这个脚本需要运行几次,参数名称假设为 --retry ,那么我们可以这样在启动器的脚本中添加如下内容:

    from airtest.cli.runner import AirtestCase, run_script
    from airtest.cli.parser import runner_parser
    
    
    class CustomAirtestCase(AirtestCase):
        def setUp(self):
            # 在air脚本运行之前获取这个自定义的参数
            if self.args.retry:
                self.scope['retry']=self.args.retry
    
    if __name__ == '__main__':
        ap = runner_parser()
        # 添加自定义的命令行参数
        ap.add_argument(
            "--retry", help="The number of times the script will be run")
        args = ap.parse_args()
        run_script(args, CustomAirtestCase)
    

    有了启动器之后,我们的 .air 脚本就可以接收到这个命令行参数并对它进行相应的处理了:

    # -*- encoding=utf8 -*-
    __author__ = "AirtestProject"
    
    from airtest.core.api import *
    auto_setup(__file__)
    
    print("脚本需要运行的次数是:"+retry)
    print('参数个数为:'+str(len(sys.argv))+'个参数')
    print('参数列表:'+str(sys.argv))
    

    带启动器的运行脚本命令

    已经准备好启动器脚本和我们的 .air 脚本之后,就可以在命令行使用如下命令,使用我们定义好的启动器来运行我们的 air 脚本啦:

    python launcher.py test.air --device Android:///serial_num --log log_path --retry 3
    

    小结

    当然,除了添加自定义的命令行参数以外,Airtest启动器的妙用还非常多,比如添加自定义的变量或者方法,添加一些前置脚本或者后置脚本等等,更多关于启动器的妙用,请同学们关注我们公众号后续的一些教程推文~


    Airtest官网http://airtest.netease.com/
    Airtest教程官网https://airtest.doc.io.netease.com/
    搭建企业私有云服务https://airlab.163.com/b2b

    官方答疑 Q 群:654700783

    呀,这么认真都看到这里啦,帮忙点个推荐支持一下呗,灰常感谢~

  • 相关阅读:
    零拷贝报文捕获平台
    Table of Contents ---BCM
    bcm cmd
    Linux常用性能调优工具索引
    Vue params传值的坑
    安装了新的angular版本后无法运行老的angular版本项目
    后端返回的数据与前端console.log数据不一致问题
    门户页跳转页面 跳转指定的页面 接口会变成路由去显示 而不是显示组件
    配置git ssh 密钥
    grafana环境变量
  • 原文地址:https://www.cnblogs.com/AirtestProject/p/14606581.html
Copyright © 2020-2023  润新知