zoukankan      html  css  js  c++  java
  • UIAutomator2-UI自动化框架搭建

    为什么选用UIAutomator2?

    Nemo社区UI自动化选用的Android UI自动化框架UIAutomator2,经使用发现,该框架有如下优点:

    1.UI自动化编写采用python,学习成本低

    2.UI自动化脚本运行稳定

    与Appium相比,又有如下突出优势:

    1.环境搭建便捷

    2.UI控件识别有专业工具,可视化好

    环境搭建
    1.jdk安装,建议版本1.80以上
    2.Android环境

    从谷歌官网下载Android Platform Tools https://developer.android.com/studio/command-line,解压,并加包含adb.exe的目录加入到系统的PATH中

    https://developer.android.google.cn/studio/releases/platform-tools

    环境安装后验证代码:

    3.Python3环境(3.6.5以上)
    UIAutomator安装
    安装要求Requirements
    • Android版本 4.4+
    • Python 3.6+
    如果用python2的pip安装,会安装本库的老版本0.2.3;如果用python3.5的pip安装,会安装本库的老版本0.3.3;两者均已经不会再维护;PYPI上的最近版本是这个:https://pypi.org/project/uiautomator2/
    1.安装 uiautomator2
    # 鉴于uiautomator2还在开发, 可以添加前缀 --pre来安装开发版本或升级现有版本
    pip install --upgrade --pre uiautomator2

    或者使用git安装:

    git clone https://github.com/openatx/uiautomator2
    pip install -e uiautomator2

    安装提示success即可。

    2.安装守护进程到手机里

    电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agentopenstf/minicapopenstf/minitouch

    # init 所有的已经连接到电脑的设备
    python -m uiautomator2 init

    有时候init也会出错,这时候需要我们手动init安装提示success即可。

    3.安装 weditor (UI查看器,编写脚本时用,相当于Appium的uiautomator viewer)

    因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,开发了基于浏览器技术的weditor UI查看器。

    安装方法(备注: 目前最新的稳定版为 0.1.0)

    pip install -U weditor
    Windows系统可以使用命令在桌面创建一个快捷方式 python -m weditor --shortcut

    命令行启动 python -m weditor 会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。

    应用及操作

     

    uiautomator2使用基本过程

    1.选择合适的方式连接手机,如usb数据线,Wi-Fi

    2.使用工具,抓去手机app的控件元素

    3.基于元素控件,调用uiautomator2 API编写UI自动化脚本

     

    手机连接方式

    1.使用WIFI连接

    手机获取到手机的IP,并确保电脑可以PING通手机。手机的IP可以在设置-WIFI设置里面获取到。华为手机无法通过Wi-Fi连接(华为系统原因)

    比如手机的IP是10.242.23.215,连接设备的代码为

    import uiautomator2 as u2 //依赖包
    d = u2.connect('10.242.23.215')

    2.使用USB连接

     

    手机的序列号可以通过adb devices获取到,假设序列号是123456f,连接代码为

    import uiautomator2 as u2
    import uiautomator2 as u2 //依赖包
    d = u2.connect('10.242.23.215')
  • 相关阅读:
    PAT Advanced 1067 Sort with Swap(0, i) (25分)
    PAT Advanced 1048 Find Coins (25分)
    PAT Advanced 1060 Are They Equal (25分)
    PAT Advanced 1088 Rational Arithmetic (20分)
    PAT Advanced 1032 Sharing (25分)
    Linux的at命令
    Sublime Text3使用指南
    IntelliJ IDEA创建第一个Groovy工程
    Sublime Text3 安装ftp插件
    Sublime Text3配置Groovy运行环境
  • 原文地址:https://www.cnblogs.com/hlsam/p/13162292.html
Copyright © 2011-2022 走看看