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


    上期回顾:Airtest通过代码生成报告——simple_report、LogToHtml详解


    以下基于
    python3.8;airtestIDE1.2.13;airtest1.2.4;pocoui1.0.85

    之前我们讲了airtest run命令行运行命令,其中有个device参数,只要输入设备的URI,会自动帮我们连接设备。那么今天就来讲一讲不通过命令行运行,在脚本中如何连接、操作设备。

    相关的API有:

    auto_setup(),connect_device(),init_device(),device(),set_current(),cli_setup()

    auto_setup()

    这个函数我们在IDE中新建文件的时候,都会自动帮我们写上。它是用来帮我们做初始化的,可以设置脚本路径、连接设备、log路径、项目根目录、截图精度。

    # 文件位置:your_python_path/site-packages/airtest/core/api.py
    def auto_setup(basedir=None, devices=None, logdir=None, project_root=None, compress=None):
        if basedir:
            if os.path.isfile(basedir):
                basedir = os.path.dirname(basedir)
            if basedir not in G.BASEDIR:
                G.BASEDIR.append(basedir)
        if devices:
            for dev in devices:
                connect_device(dev)
        if logdir:
            logdir = script_log_dir(basedir, logdir)
            set_logdir(logdir)
        if project_root:
            ST.PROJECT_ROOT = project_root
        if compress:
            ST.SNAPSHOT_QUALITY = compress

    参数说明:

    • basedir:当前脚本路径,默认为None,可设置为__file__

    • devices:字符串列表[],里面放设备URI字符,之前Airtest命令行运行airtest run详解 中的--device参数解释有各平台的示例。因为是列表,所以你可以传入2个URI,这样将连接两台设备

    • logdir:日志存放路径。默认为None表示不保存日志;可指定路径字符串;也可设置为True,则自动设置路径为<basedir>/log

    • project_root:项目根目录,用于赋值给全局设置ST.PROJECT_ROOT,方便using()接口的调用

    • compress:截图精度,1-100;默认值为None时精度是10

    源码解析:

    1. 第1个if,将脚本路径保存在全局变量G.BASEDIR中

    2. 第2个if,依次用connect_device()连接设备URI

    3. 第3个if,设置日志存放路径

    4. 第4个if,将项目根目录保存在全局设置ST.PROJECT_ROOT

    5. 第5个if,将截图精度保存在全局设置ST.SNAPSHOT_QUALITY,ST.SNAPSHOT_QUALITY的默认值是10

    可以看到auto_setup()连接设备其实就是调用的connect_device()

    演示实例

    __author__ = '公众号:测试工程师小站'

    # 连接模拟器
    auto_setup(__file__, devices=["android://127.0.0.1:5037/emulator-5554?cap_method=JAVACAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH"], 
    logdir=True, project_root=r"D:\qasite", compress=90)

    connect_device()

    # 文件位置:your_python_path/site-packages/airtest/core/api.py
    def connect_device(uri):
        d = urlparse(uri)
        platform = d.scheme
        host = d.netloc
        uuid = d.path.lstrip("/")
        params = dict(parse_qsl(d.query))
        if host:
            params["host"] = host.split(":")
        dev = init_device(platform, uuid, **params)
        return dev

    参数说明:

    • uri:设备连接字符串

    源码解析:
    前面的语句就是各种解析URI,拆解出各部分并存入相应变量。核心就是调用了init_device()初始化设备。

    演示实例

    __author__ = '公众号:测试工程师小站'

    # 连接安卓设备
    connect_device("Android:///65fade15")

    # 连接iOS设备(通过tidevice连接,详情可以查阅之前的文章)
    connect_device(""ios:///http+usbmux://07bbb06a267ee"")

    # 连接Windows窗口
    connect_device("Windows:///?title_re=.*记事本.*")

    # 连接模拟器
    connect_device("Android://127.0.0.1:5037/127.0.0.1:62001?cap_method=JAVACAP&&ori_method=ADBORI")

    # 连接多台设备
    dev1 = connect_device("Android://127.0.0.1:5037/serialno1")  # 连上第一台手机
    dev2 = connect_device("Android://127.0.0.1:5037/serialno2")  # 连上第二台手机
    set_current("serialno2")  # 切换当前手机为serialno2的手机

    init_device()

    # 文件位置:your_python_path/site-packages/airtest/core/api.py
    def init_device(platform="Android", uuid=None, **kwargs):
        cls = import_device_cls(platform)
        dev = cls(uuid, **kwargs)
        # Add device instance in G and set as current device.
        G.add_device(dev)
        return dev

    参数说明:

    • platform:Android, IOS or Windows

    • uuid:Android的序列号(adb deivces), Windows的窗口句柄, iOS的uuid(tidevice list)

    • **kwargs:可选的平台相关参数,如安卓的cap_method=JAVACAP

    源码解析:
    第1行,确定要初始化的类,摘取部分import_device_cls()源码,主要就是这3个平台的类

        elif platform == "android":
            from airtest.core.android.android import Android as cls
        elif platform == "windows":
            from airtest.core.win.win import Windows as cls
        elif platform == "ios":
            from airtest.core.ios import IOS as cls

    第2行,实例化一个设备
    第3行,将实例化的设备添加到全局设备列表中(G.DEVICE_LIST),并设置为当前使用设备(G.DEVICE)

    device()

    获取当前设备,很简单,源码就一行return G.DEVICE
    有了设备变量后,我们就可以使用各种Airtest API了,如

    dev = device()
    dev.touch((100, 100))

    set_current()

    上面讲auto_setup()和connect_device()时都有提到,我们可以初始化两台设备,但同一时间,只有一台设备是激活状态可操作的。所以操作完一台后,我们要切换到第2台去操作,就可以用set_current()

    __author__ = '公众号:测试工程师小站'

    from airtest.core.api import *

    # 初始化2台设备,用auto_setup()或connect_device()都可以
    # auto_setup(__file__, devices=["android:///serialno1","android:///serialno2"], logdir=True)
    # 或
    dev1 = connect_device("Android://127.0.0.1:5037/serialno1")
    dev2 = connect_device("Android://127.0.0.1:5037/serialno2")

    # 通过序列号切到手机1
    set_current("serialno1")

    # 打开微信A,添加微信B好友(代码略)

    # 通过序列号切到手机2
    set_current("serialno2")

    # 打开微信B,同意微信A好友申请(代码略)

    # 通过设备列表索引切到手机1
    set_current(0)

    # 在微信A,向微信B发消息(代码略)

    # 通过设备列表索引切到手机2
    set_current(1)

    # 在微信B,断言收到微信A的消息(代码略)

    注意:使用set_current()后,一定要加比较长的等待,如sleep(10),因为底层的一系列切换操作比较慢,不加等待会导致运行失败。

    cli_setup()

    在IDE新建 .py 脚本时,会自动在脚本中插入如下的代码

    __author__ = '公众号:测试工程师小站'

    from airtest.core.api import *
    from airtest.cli.parser import cli_setup

    if not cli_setup():
        auto_setup(__file__, logdir=True, devices=["Android:///",])

    cli_setup()的作用就是脚本运行后判断是不是通过命令行运行的,如果不是,则运行auto_setup()来初始化设备等,如果是,则通过命令中的参数初始化。
    这样,此脚本既可以直接在AirtestIDE或Pycharm运行,也可以通过airtest命令行运行了。

    ---------------------------------------------------------------------------------

    关注微信公众号即可在手机上查阅,并可接收更多测试分享~

  • 相关阅读:
    C 语言高效编程的几招——A few action of efficient C language programming
    UDP套接字——(DGRAM)
    初学数位DP--hdu 2089
    leetcode Reverse Nodes in k-Group
    CC+语言 struct 深层探索——CC + language struct deep exploration
    [置顶] JDK工具(一)–Java编译器javac
    非归档数据文件offline的恢复
    [置顶] OpenJDK源码研究笔记(九)-可恨却又可亲的的异常(NullPointerException)
    MSF溢出实战教程
    一些安全名词解释
  • 原文地址:https://www.cnblogs.com/songzhenhua/p/15863868.html
Copyright © 2020-2023  润新知