Skip to content
鼓励作者:欢迎打赏犒劳

App自动化 ​

需要的环境

adb安装和使用 ​

很简单,直接下载解压,然后配置到环境变量即可。

测试命令

shell
D:\devtool\platform-tools>adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached

adb常用命令 ​

查看转发命令

shell
adb forward --list

常用场景 ​

获取手机设备信息 ​

shell
import uiautomator2 as u2

#连接手机 adb
device = u2.connect('MJE0224709000645')

#打开被测试的app
print(device.device_info)
print(device.info) # 这个信息更多


#获取屏幕大小 数据类型是一个元组
print(device.window_size())
#截屏 可以传路径
device.screenshot(r'd:\test.png')
#推送文件
device.push(r"d:\demo.txt",'/data/')
#拉取文件
device.pull('/data/demo.txt', r'd:\data\1.txt')

获取app包名 ​

shell
# 获取当前正在运行的app
print(device.app_list_running())
# 获取所有在运行的app 结果是一个集合
print(device.app_current())

打开软件 ​

shell
import uiautomator2 as u2

#连接手机 adb
device = u2.connect('emulator-5554')

print(device.device_info)

#打开被测试的app
device(text='淘宝').click()

下载安装/卸载 ​

shell
import uiautomator2 as u2

#连接手机 adb
device = u2.connect('emulator-5554')

print(device.device_info)

#apk下载地址
# url = "https://xxxxxxxxxx.apk"
# device.app_install(url)
# 回到桌面
device.press("home")
sleep(2)
## 打开app
device(text='墨玉甄选').click()
sleep(2)
#获取app的包名()  {'package': 'top.seegood.toapp.myapp', 'activity': 'com.example.template.MainActivity', 'pid': 0}
print(device.app_current())


#卸载
device.app_uninstall(device.app_current()['package'] )

device.press("home")

下面的才是正确的使用方式,上面的代码有bug,主要是为了让大家熟悉下api

shell
TARGET_PKG = 'top.seegood.toapp.myapp'

device.press("home")
device.app_start(TARGET_PKG,wait=True)          # 会等待启动
print("前台:", device.app_current()['package'])

#数据清空
device.app_clear('com.ss.android.article.browser')

ok = device.app_uninstall(TARGET_PKG)
print("卸载结果:", ok)
device.press("home")

元素定位 ​

下载辅助工具uiautodev

shell
pip install uiautodev
python -m uiautodev

image-20261129-220316-1.png

python
from time import sleep

import uiautomator2 as u2

#连接手机 adb
device = u2.connect('emulator-5554')


TARGET_PKG = 'top.seegood.toapp'

device.press("home")
device.app_start(TARGET_PKG)          # 会等待启动
print("打开软件:", device.app_current()['package'])

# xpath查找元素 点击
device.xpath("//*[@content-desc='我的']").click()


#根据上下级关系去进行定位
my_icon = device(resourceId='com.cc.xx:id/navigationmy').child(resourceId='com.cc.xx:id/icon')
my_icon.click()

# 同级元素
text_view = my_icon.sibling(className='android.view.View')
text_view.click()

元素滑动 ​

python
# 滑动操作4个参数:
device.swipe(800, 500,100,500)
# 扩展版的滑动操作 一般都用这个,意思是向左滑,滑0.9的屏幕大小
device.swipe_ext('left', scale=0.9)

用户输入清空 ​

python
device(resourceId='com.cc.xx:id/et_mobile').send_keys('1833393333')
device(text='登录').click()
time.sleep(3)
device(resourceId='com.cc.xx:id/et_mobile').clear_text()

截图操作 ​

python
# 第一种
device.screenshot(r'd:\test.png')

# 第二种 进阶版 可以处理图片
im = device.screenshot()
im.save("hello.png")
# 模糊图片
im2 = im.filter(ImageFilter.BLUR)
# 设置大小
im3 = im.resize((90, 160))
im2.save("hellocpy.png")
im3.save ("hellosmall.png")

等待 ​

  • 实例2:d.app_start("", wait=True),不需要加 time.sleep()
  • 等待某一个页面加载完成:d.wait_activity(".ui.LauncherUI", timeout=10) , 返回一个布尔值进行判断
  • 等待元素出现:d(text="个人中心").wait(timeout=15) , 等待登录后的“个人中心”页面元素出现,最多等 15 秒
  • 等待元素消失:d(text="加载中...").wait_gone(timeout=30)
  • 等待/判断元素是否存在:d(text="我知道了").exists 返回一个布尔值进行判断
  • 设置全局等待时间 d.settings['wait_timeout'] = 5.0

上次更新:

如有转载或 CV 的请标注本站原文地址