如果卡在connect()
kill掉python进程
拔掉USB且关闭开发者,再接上USB,再重新打开开发者
adb devices
adb shell
/data/local/tmp/atx-agent server -d
启动一下atx
然后重新运行脚本就不会卡connect()了
# 一.UIAutomator2安装和基本API
'''
判断是原生app还是混合app
1.直接问开发
2.开发者选项->绘图(显示布局边界勾起来)
如果app里面每个界面的每个控件都是独立的小小的框框就是原生
只要有一个大框里面有很多控件就是混合H5的
#获取当前界面信息
python -m uiautomator2 current
因为xpath有一些问题,容易卡住,因此换为Id或者text desc定位
使用device(resourceId='',text='')单个或者组合定位元素
'''
# 默认收藏元素界面
# app.splash.SplashActivity bbf5382
# 播放列表界面
# app.splash.SplashActivity
# 1.导入uiautomator2库
# 因为导入的外部的库,没有的话要pip install uiautomator2安装一下
import uiautomator2 as u2
import time
def test_play_music():
# 2.连接android设备
# connect需要使用到adb,如果没有adb环境的话要下载,并配置环境变量
# device = u2.connect('192.168.50.66')
print('---')
try:
# 默认用usb连接
device = u2.connect()
# device = u2.connect('192.168.1.7:40443', timeout=30) # 默认10秒延长至30秒
print(device.info)
current = device.app_current()
print(current['package']) # 输出当前包名
print(current['activity']) # 输出当前Activity
# # d.wait_timeout = 5
device.implicitly_wait(10)
print(0)
device.screenshot("screen0.png") # 保存为当前目录下的screen.png
# 清空app数据,包括登录数据,变为未登录状态
# device.app_clear('com.kugou.android')
# # 酷狗
# device.xpath('//*[@text="酷狗音乐"]').click()
device.app_start('com.kugou.android',wait=True)
print(1)
time.sleep(5)
# device.screenshot("screen1.png") # 保存为当前目录下的screen.png
# 我的
# mine = device.xpath('//*[@content-desc="我的"]')
device(resourceId='com.kugou.android:id/bg3',text='我的').click()
print(1.5)
print('点击我的')
time.sleep(2)
# device.screenshot("screen2.png") # 保存为当前目录下的screen.png
# time.sleep(10)
# 默认收藏
# 计算中点并点击
x_center = (228 + 948) // 2
y_center = (1710 + 1908) // 2
print(x_center,y_center)
# print(x_center,y_center)
device.click(x_center, y_center)
time.sleep(2)
print('点击默认收藏')
print(3)
print('这里很奇怪,先睡3s解决')
time.sleep(3)
# 这里直接到播放的界面,温泉是文字,无法click
print(99)
device(resourceId='com.kugou.android:id/f6m1',text='温泉').click()
time.sleep(2)
print(4)
# 未播放时点击播放
if device(resourceId='com.kugou.android:id/db51',description='暂停').exists():
print(4.5)
device(resourceId='com.kugou.android:id/db51',description='暂停').click()
print('点击暂停')
print(4.6)
else:
print('点击播放,睡5s')
device(resourceId='com.kugou.android:id/db51',description='播放').click()
time.sleep(5)
print(4.9)
# device.xpath('//*[@content-desc="暂停"]').click()
print('点击暂停')
print(5)
time.sleep(2)
# 未播放时点击播放
if device(resourceId='com.kugou.android:id/db51',description='暂停').exists():
print(5.5)
device(resourceId='com.kugou.android:id/db51',description='暂停').click()
print('点击暂停')
print(5.6)
else:
print('点击播放,睡5s')
device(resourceId='com.kugou.android:id/db51',description='播放').click()
time.sleep(5)
print(5.9)
print(6)
time.sleep(2)
except Exception as e:
print('异常:')
print(e)
print()
finally:
import subprocess
subprocess.run(["adb", "shell", "am", "force-stop", "com.github.uiautomator"]) # 强制停止服务进程:ml-citation{ref="1" data="citationList"}
device.app_stop('com.kugou.android')
time.sleep(1) # 等待服务停止
print("连接已关闭")
for i in range(1,101):
print(f'第{i}次,播放:')
test_play_music()
# com.kugou.android
# app.splash.SplashActivity
# 3.通过点击打开被测软件‘探探’
# 如果没有的话,先安装一个探探app
# device(text='QQ').click()
# # 3.2通过包名直接打开app
# device.app_start('com.xxx')
# # 4.通过包名直接关闭app
# device.app_stop('com.xxx')
# # 5.通过包名清除app数据
# device.app_clear('com.xxx')
# # 6.获取当前app包名
# app_current = device.app_current()
# # 7.删除app通过包名
# devcies.app_uninstall(app_current)
# # 获取活动名和包名的adb指令
# adb1 = 'adb shell dumpsys activity | find "mFocusedActivity" '
# adb1 = 'adb shell dumpsys activity top | findstr ACTIVITY'
# # 手上有apk,直接判断apk的包名是什么。
# # aapt dump badging file.apk
# # 8.连接上打印一下信息
# device.info
# device.device_info
# # 9.截屏
# device.screenshot(r'd:\test.png')
# # 10.推送文件
# devcie.push(r'd:\test.png','/data/')
# #11.拉取文件 (后面一定要带上名称)
# device.pull('/data/test.png',r'd:\test.png')
# # 12.按键,用的时候去查找就行了,记得几个,上下左右,home,back,OK就可以了
# # 二。元素定位工具 WEditor
# # 最后一行有直接可以使用的代码直接拷贝 类似 d(resourceId="")
# # 13.可以通过组合的方式,也可以通过实际上是唯一的方式找到控件
# device(resourceId='com.lemon.xxx',text='我的柠檬')
# # 14.输入操作
# mobile_elem = device(resourceId='')
# mobile_elem.send_keys('1643')
# d(text='登录').click()
# time.sleep(3)
# # 清空数据
# mobile_elem.clear_text()
# mobile_elem.send_keys('18246346124')
# # 15.等待方式
# time.sleep(5)
# # 这两种方式效果一样,一种通过属性修改,另一种直接是函数的方式
# d.wait_timeout = 5
# d.implicitly_wait(5)
# # 默认20s
# d.app_start('com.aaa.xxx', wait =True)
# # 16.等待元素出现,消失,是否存在
# d(text='demo').wait()
# d().wait_gone()
# d().exist()
# # 然后再click 或者 clear_text()
# # 如果比全局还要长
# # 50s内点击成功就可以
# d().click(timeout=50)
# d().set_text(timeout=50)
# d().clear_text(timeout=50)
完全xpath,好像有bug
# 一.UIAutomator2安装和基本API
'''
判断是原生app还是混合app
1.直接问开发
2.开发者选项->绘图(显示布局边界勾起来)
如果app里面每个界面的每个控件都是独立的小小的框框就是原生
只要有一个大框里面有很多控件就是混合H5的
python -m uiautomator2 current
'''
# 默认收藏元素界面
# app.splash.SplashActivity bbf5382
# 播放列表界面
# app.splash.SplashActivity
# 1.导入uiautomator2库
# 因为导入的外部的库,没有的话要pip install uiautomator2安装一下
import uiautomator2 as u2
import time
def test_play_music():
# 2.连接android设备
# connect需要使用到adb,如果没有adb环境的话要下载,并配置环境变量
# device = u2.connect('192.168.50.66')
print('---')
try:
device = u2.connect()
print(device.info)
current = device.app_current()
print(current['package']) # 输出当前包名
print(current['activity']) # 输出当前Activity
# # d.wait_timeout = 5
device.implicitly_wait(10)
print(0)
device.screenshot("screen0.png") # 保存为当前目录下的screen.png
# 清空app数据,包括登录数据,变为未登录状态
# device.app_clear('com.kugou.android')
# # 酷狗
# device.xpath('//*[@text="酷狗音乐"]').click()
device.app_start('com.kugou.android',wait=True)
print(1)
time.sleep(5)
# device.screenshot("screen1.png") # 保存为当前目录下的screen.png
# 我的
# mine = device.xpath('//*[@content-desc="我的"]')
mine = device.xpath('//*[contains(@text,"我的")]')
print(1.5)
mine.click()
print('点击我的')
time.sleep(2)
# device.screenshot("screen2.png") # 保存为当前目录下的screen.png
# time.sleep(10)
# 默认收藏
collect = device.xpath('(//android.widget.LinearLayout)[63]')
# if collect.wait(timeout=10.0):
collect.click()
print('点击默认收藏')
time.sleep(2)
# device.screenshot("screen2.png") # 保存为当前目录下的screen.png
print(3)
print('这里很奇怪,先睡3s解决')
time.sleep(3)
# 这里直接到播放的界面,温泉是文字,无法click
print(99)
two_p = device.xpath('//android.widget.TextView[@text="许嵩、刘美麟"]')
print(99)
# 播放音乐
if two_p.exists:
print(4.5)
two_p.click()
print('点击播放')
print(5)
else:
# device.screenshot("error.png") # 保存为当前目录下的screen.png
raise Exception('no 点击播放歌曲')
time.sleep(3)
# device.screenshot("screen10.png") # 保存为当前目录下的screen.png
print(4)
# 未播放时点击播放
if device.xpath('//*[contains(@text,"播放")]').exists:
print(4.5)
device.xpath('//*[contains(@text,"播放")]').click()
print('点击播放')
print(5)
else:
print('睡5s 然后点击暂停')
time.sleep(5)
device.xpath('//*[@content-desc="暂停"]').click()
print('点击暂停')
time.sleep(3)
# 未播放时点击播放
if device.xpath('//*[@resource-id="com.kugou.android:id/db51"]').exists and device.xpath('//*[@content-desc="播放"]').exists:
print(5.1)
device.xpath('//*[@content-desc="播放"]').click()
print('点击播放')
print(5.1)
elif device.xpath('//*[@resource-id="com.kugou.android:id/db51"]').exists and device.xpath('//*[@content-desc="暂停"]').exists:
print('睡5s 然后点击暂停')
time.sleep(5)
device.xpath('//*[@content-desc="暂停"]').click()
print('点击暂停')
time.sleep(3)
# 未播放时点击播放
if device.xpath('//*[@resource-id="com.kugou.android:id/db51"]').exists and device.xpath('//*[@content-desc="播放"]').exists:
print(5.2)
device.xpath('//*[@content-desc="播放"]').click()
print('点击播放')
print(5.2)
elif device.xpath('//*[@resource-id="com.kugou.android:id/db51"]').exists and device.xpath('//*[@content-desc="暂停"]').exists:
print('睡5s 然后点击暂停')
time.sleep(5)
device.xpath('//*[@content-desc="暂停"]').click()
print('点击暂停')
# device.app_stop('com.kugou.android')
except Exception as e:
print('异常:')
print(e)
print()
finally:
# device.uiautomator.stop() # 替代旧的 device.service("uiautomator").stop()
# device.atx_agent.stop() # 关闭设备端服务:ml-citation{ref="3,6" data="citationList"}
time.sleep(1) # 等待服务停止
device.app_stop('com.kugou.android')
time.sleep(1) # 等待服务停止
print("连接已关闭")
for i in range(1,101):
print(f'第{i}次,播放:')
test_play_music()