前言

本文章需要会 python 基础语法,课程目标是熟练使用 uiautomator2 库进行 android 自动化测试。


一、环境准备

1.安装软件

  • python3
  • pycharm社区版
  • 安卓手机或模拟器
  • adb
    提示:需要配置相应的环境变量

2.安装库

##打开cmd面板,安装以下库
pip install uiautomator2
pip install setuptools

二、adb 连接手机

1. 准备工作

  1. 在“设置-我的设备-全部参数”找到手机版本,连续点击7次会进入开发者模式
  2. 进入开发者选项页面,把USB调试、USB安装都打开,然后才能正常的进行连接。

2. 第一种连接方式:USB连接

需要用可以传输数据的数据线连接手机和电脑,会弹窗选择连接方式(仅充电、传文件等等),选择传文件。然后用adb命令查看连接设备 adb devices -l ,能看到设备信息就说明已连接到设备可以正常调试设备了,否者可能是开发者模式没打开。

adb devices -l 

3. 第二种连接方式:WLAN连接

  1. 这种方式适用于Android版本10及一下的手机,需要借助USB。首先要确保手机和电脑在同一个局域网内(连接同一个WiFi),其次需要在开发者选项里面将无线连接打开,进入可以查看到手机设备的IP,并且通过USB连接手机和电脑。
  2. 用adb命令设置手机监听 adb tcpip 5555
#需要usb连接手机才能开启
adb tcpip 5555 
  1. 然后就可以拔掉USB了,执行命令adb connect 手机ip:5555
#通过手机无线网查看ip地址
adb connect 192.168.1.178:5555
  1. 然后用adb命令查看连接设备 adb devices -l ,能看到设备信息就说明已连接到设备可以正常调试设备了
adb devices -l 

4. 第三种连接方式:WiFi连接

  1. 这种方法适用于Android版本11及以上,adb --version ≥ 30.0.0的条件使用。首先要确保手机和电脑在同一个局域网内(连接同一个WiFi),要把开发者模式和无线调试模式,选择使用配对码配对,需要记下配对码、IP 地址和端口号。
#查看 adb 版本号
adb --version
  1. 运行命令adb pair 手机ip:端口号,进入下一步,根据提示输入配对码,系统会显示配对成功
adb pair 192.168.1.179:45893
# Enter pairing code: 840322
  1. 执行命令adb connect 手机ip:端口号 ;
# 和配对的端口号不一样
adb connect 192.168.1.179:39635
  1. 然后用adb命令查看连接设备 adb devices -l ,能看到设备信息就说明已连接到设备可以正常调试设备了。
adb devices -l 

5.查看端口转发

adb forward --list

三、uiautomator2 连接手机

1. 前期准备

打开手机软件 ATX,启动 UIAUTOMATOR

2. 默认连接

import uiautomator2 as u2
#仅当只一个的时候可以使用
device = u2.connect()
#通过序列号连接
device = u2.connect('U4HA6HXK7XEY65BY') 
#通过wifif去接链接
device = u2.connect('http://192.178.1.178:7912')
device = u2.connect('http://192.178.1.178')

3. WIFI 地址连接

import uiautomator2 as u2
device = u2.connect_wifi('192.178.1.178:7912')
device = u2.connect_wifi('192.178.1.178')

4. 解决 Uiautomator not starting 问题

先试用adb devices查询到设备
再使用命令python -m uiautomator2 init在模拟器上安装ATX
打开ATX,启动UIautomator,提示Uiautomator not starting,
解决办法:

adb shell
chmod 755 /data/local/tmp/atx-agent
data/local/tmp/atx-agent version # 查看版本
/data/local/tmp/atx-agent server -d # 启动atx-agent并切换到后台运行

参考链接:https://www.jianshu.com/p/b002f4505aee

四、uiautomator2 的使用

1. 安装、卸载

import uiautomator2 as u2
device = u2.connect("emulator-5554")
#APP柠檬浏览器 的下载地址
url = "https://dl.byhh.net:2333/6_83698"
#安装 APP柠檬浏览器
device.app_install(url)
# 打开APP
device(text='柠檬浏览器').click()
# 获取APP信息
appInfo = device.app_current()
print(appInfo )
# 卸载 APP(包名)
device.app_uninstall("com.ss.android.article.browser")

2. 启动、关闭、清除

import uiautomator2 as u2
device = u2.connect("emulator-5554")
# 打开APP
device.app_start("com.ss.android.article.browser")
# 关闭 APP
device.app_stop("com.ss.android.article.browser")
# 清除APP 数据
device.app_clear("com.ss.android.article.browser")

3. 获取APP包名

# 1.通过代码获取APP包名
# 获取当前运行的APP包名
appInfo = device.app_current()
# 获取正在运行的APP 所有包名
appInfos = device.app_list_running()

4. 设备操作

import uiautomator2 as u2
device = u2.connect("emulator-5554")
# 获取设备相关信息
info = device.info
# 获取设备更多信息
deviceInfo = device.device_info
# 屏幕大小
windosSize = device.window_size()
# 截屏操作
device.screenshot("test.png")
# 将电脑文件推送到手机
device.push('test.txt','/data/')
# 将手机文件拉取到电脑
device.pull('/data/test.txt','./text2.txt')

5. 按键操作

import uiautomator2 as u2
device = u2.connect("emulator-5554")
# 亮屏
device.screen_on()
# 熄屏
device.screen_off()
# 音量加/减
device.press("volume_up")
device.press("volume_down")
# 静音
device.press("volume_mute")
# 最近的程序
device.press('recent')
# 电源键
device.press("power")
## 其它对应的press 可进行百度查询

五、元素

1. 元素定位辅助工具weditor

# 方法一:安装weditor 
pip install weditor
# 方法二:安装weditor
git clone https://github.com/alibaba/web-editor
pip3 install -e web-editor
# 启动 weditor
weditor

weditor 对比其它工具:uiautomatorviewer

  • 可以周管理多个设备
  • 直接在调试界面操作手机
  • 自动生成uiauotmator代码

2. 元素(控件)构成

  • 控件名称
  • 控件属性(特征)resourceId,className,description,text 等
  • 子控件

3. 元素(控件)定位方式

  • text,textContains,textMatches,textStartswith
  • className,classNameMatches
  • description,descriptionContains,descriptionMatchs,descriptionStartwith
  • checkable,enabled,clickable,longclickable
  • scrollable,enabled,forcusable,focused,selected
  • packageName,packageNameMatches
  • resourceId,resourceIdMatches
  • index,instance

提示:Contains(包含)Matches(正则表达式)Startswith(开头是)instance(索引)

4. 通过上下级关系去定位元素

import uiautomator2 as u2
d = u2.connect("emulator-5554")
# child 子元素
d(className="android.view.ViewGroup").child(text="柠檬浏览器").click()
# sibling 同级元系
d(text="ATX").sibling(text="柠檬浏览器").click()
# 根据页相对位置(速度较慢)
# left,左边的元素
d(text="ATX").left().click()
# right,右边的元素
# up,上面的元素
# down,上面的元素

5. 点击操作

import uiautomator2 as u2
d = u2.connect("emulator-5554")
# 1.通过定位元素点击
d(text="柠檬浏览器").click()
# 2.通过全局坐标点击
d.click(189,234)
# 3.通过百分比点击
d.click(0.123,0.934)

6. 滑屏操作

import uiautomator2 as u2
d = u2.connect("emulator-5554")
# 像素位置(startX, startY, endX, endY)
d.swipe(800,500,100,500)
# 滑动百分比(方向,百分比)
d.swipe('left',scale = 0.9)
# 定位元素,再使用元素对象滑动 (方向,滑动时间)
e = d(text="柠檬浏览器")
e.swipe('left',steps= 100)

7. 输入和清空

# 输入
d.send_keys("1234566")
# 睡眠
d.sleep(3)
# 清空
d.clear_text()

8. 截屏操作

import uiautomator2 as u2
d = u2.connect("emulator-5554")
from PIL import IMAGEFILTER
# 1.直接截屏
d.screenshop('dest.png')
# 2.截屏对像 pillow
img = d.screenshop()
img.save('test.png')	#保存原始图片
img2 = img.filter(ImageFIlter.BLUR) # 模糊处理
img2.save('test.png')
img3 = img.resize((90,169)) #尺寸处理
img3.save('test.png')

9. 元素等待

# 睡眠
time.sleep(5)

# 等待APP加载完成(wait默认超时时间20s)
# d.wait_timeout = 30 //也可以设置默认超时间
# d.implicitly_wait(30) //也是设置默认时间
d.app_start("com.xxx.fave",wait=True)
# 等待页面加载完成
d.wait_activity()
# 等待无素出现
d().wait()
# 等待元素消失
d().wait_gone()
# 判元素是否存在
d().exist()
# 操作单独设置超时,一般不需要设置
d().click(timeout=50)
d().set_text(timeout=50)
d().clear_text(timeout=50)
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐