Skip to content

atx 测试框架


目录

  • ATX 介绍
  • Uiautomator2 介绍
  • Uiautomator2 安装
  • Uiautomator2 工作原理
  • Uiautomator2 常用命令
  • Weditor用法介绍
  • Uiautomator2 常用Api
  • Uiautomator2 实战

ATX介绍


Uiautomator2 介绍


Uiautomator2 安装

pip instal -U uiautomator2
pip3 install -U uiautomator2

Uiautomator2 工作原理


Uiautomator2 常用命令

python3 -m uiautomator2   帮助文档
uiautomator2   version 获取版本号
uiautomator2   init 做一些初始化的操作,初始化手机设备
uiautomator2 current  获取当前的包名和activity的名字
uiautomator2 install   路径/apk包名     安装
uiautomator2 uninstall  package名      卸载
uiautomator2 start 包名   启动应用
uiautomator2 stop 包名   停止应用运行
Uiautomator2 purge  卸载初始化安装的应用

Uiautomator2 常用Api


设备连接

方法一:目前电脑上只连接一台设备
d = uiautomator2.connect()
方法二:目前电脑上连接多台设备
d = uiautomator2.connect(serialNo)
方法三:通过wifi连接设备
d = uiautomator2.connect_wifi(‘IP:port’)

设备操作

操作Api 描述
d.info 获取设备的信息(屏幕分辨率,厂商名,SDK版本等)
d.device_info 获取更详细的设备信息
d.window_size() 屏幕尺寸
d.push() 将本地的文件push 到手机上
d.pull() 将手机上的文件pull 到电脑上

app 操作

操作Api 描述
app_start(包名, 页面名) 启动应用,或者启动指定的应用的某个页面
app_stop(包名) 关闭应用
app_uninstall(package-name) 卸载应用
app_install(apk-path) 安装apk 或者 下载并安装apk
app_current() 获取当前的app名
app_clear(包名) 清空包缓存
app_list_running() 列出所有运行中的包

按键操作

操作Api 描述
d.press(“home") 模拟home 键
d.press(“back") 模拟返回键

元素定位


元素定位工具介绍

  • weditor 官方推荐使用
  • uiautomatorviewer(< Android 8.0)
  • Appium Inspector

weditor 安装

pip install -U weditor

weditor 启动

  • 方法一:命令行输入 webitor,自动打开浏览器
  • 方法二:双击桌面上的 webitor 快捷方式 (windows)
  • 注意:想要加载页面,还需要在手机端安装atx uiautomator2 init

weditor 介绍


元素定位

  • ID定位
  • 文本定位
  • XPath定位
  • className定位
# id 定位
d(resourceId="android:id/list")
# 文本定位
d(text="Google")
# 文字包含
d(textContains='搜索').click()
# XPath定位
d.xpath("//*[@content-desc='分享']")
# className定位
d(className="android.widget.ListView")

元素定位


高级定位技巧

  • .child() 子结点/ 子孙结点
  • .sibling 兄弟结点
  • d(text='Clock', className='android.widget.TextView') 选择结点文字为clock 并且 classn属性为android.widget.TextView的元素
# 多条件定位
d(text='Clock', \
    className=‘android.widget.TextView’)
# 父子关系,祖孙辈关系
d(className='android.widget.LinearLayout')\
    .child(textContains='29.2').click()
# 兄弟关系
d(className='android.widget.LinearLayout')\
    .sibling(className="android.widget.FrameLayout").click()

高级定位技巧-xpath

  • 支持使用xpath 元素定位
  • 官网

判断元素存在

# 判断元素存在
d(text="Settings").exists
d.exists(text="Settings") 
# 在规定时间内等待元素出现
d(text="Settings").exists(timeout=3) 
d(text="Settings").wait_gone(timeout=1.0)

app 常用操作api

  • 点击元素:d().click()
  • 点击坐标:d.click(x,y)
  • 长按坐标:d.long_click(x,y,duration)
  • 双击坐标:d.double_click(x, y)
  • 输入内容:d.send_keys(text)
  • 清空内容:d.clear_text()
  • 滑动:d.swipe(startx, starty, endx, endy)
  • 截图:d.screenshot(‘./images/demo1.png’)

智能等待

  • 强制等待
  • 隐式等待
  • 局部设置隐式等待
# 强制等待
time.sleep(time)
# 隐式等待
# 全局设置隐式等待时长
方法一:d.implicitly_wait(10.0)
方法二:d.settings['wait_timeout'] = 10.0
# 局部设置隐式等待
d.wait_activity(“.MainActivity”, timeout=10)

特殊控件 toast

d.toast.get_message(timeout, cachetime, message)

弹框监控

# 定义监控
d.watcher.when("//*[@text = ‘同意']").click()
# 开始监控
d.watcher.start()    开始监控
d.watcher.start(2.0) 默认监控间隔2.0s
d.watcher.run()   强制运行所有监控
# 移除监控
d.watcher.remove()
# 停止监控
d.watcher.stop()

Uiautomator2 企业微信实战 - 添加联系人

  • 关闭所有应用
  • 打开企业微信
  • 点击【通讯录】
  • 点击【添加成员】
  • 点击【手动输入添加】
  • 等待【保存】显示出来
  • 输入姓名【hogwarts】
  • 输入手机号【13100000000】
  • 点击【保存】
  • 验证添加成功
  • 关闭企业微信