pythonturtle使用方法:python UIAutomator2使用超详细教程
pythonturtle使用方法:python UIAutomator2使用超详细教程点击确定保存,配置完成。并加包含adb的目录加入到系统的PATH中。设置系统环境变量。上面简介大家自行查找 简而言之 就是在PC上得程序可以连接安卓手机进行操控。话不多说直接上干货。从谷歌官网下载 Android Platform Tools, 解压。
一、简介
uiautomator2是一个Python库,用于Android的UI自动化测试,其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作。
GitHub地址:https://github.com/openatx/uiautomator2
二、支持平台及语言python-uiautomator2封装了谷歌自带的uiautomator2测试框架,提供便利的python接口。他允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,大大提高了自动化代码编写的效率。
上面简介大家自行查找 简而言之 就是在PC上得程序可以连接安卓手机进行操控。
话不多说直接上干货。
安装adb:从谷歌官网下载 Android Platform Tools, 解压。
并加包含adb的目录加入到系统的PATH中。设置系统环境变量。
点击确定保存,配置完成。
安装UIAutomator2 模块pip install --pre uiautomator2
pip install pillow (如果需要截图,可安装pillow)
安装weditor
有了这个,方便我们快速识别手机上的元素,方便写代码
pip install -U weditor #pip install --pre weditor
#Windows系统可以使用命令在桌面创建一个快捷方式:
python -m weditor –shortcut
在这里PC环境搭建完毕。
接下来设置手机配置。
打开手机的开发者功能,并开启调试功能。
这里是华为P20鸿蒙系统。
有部分用户手机是安卓11以上的系统,有无线调试功能。当然这个就很棒了。可以开启无线调试功能。
手机设置好之后。插上usb与电脑链接.
第一步查找自己的手机链接驱动名称
#第一次运行 adb devices 命令
adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached
#第二次运行 adb devices 命令
adb devices
List of devices attached (没有找到连接手机)
#插上usb线之后 继续执行adb devices
adb devices
List of devices attached
D5F0218314002832 device
#D5F0218314002832 就是手机连接电脑的驱动名称
#后面device是连接状态.这个是连接正常.
#网上很多方法说 在连接手机之后先要初始换手机环境之类的,我觉得这部分没必要在命令中写.
#初始化这部分可以在python中进行初始化。
python -m uiautomator2 init
直接上python代码:
import adbutils
import logging
import uiautomator2 as u2
#这里会给手机安装部分软件ATX.总共安装五个程序.
#minicap
#inicap.so
#minitouch
#com.github.uiautomator
#com.github.uiautomator.test
#开始安装
def init():
device = adbutils.adb.device("D5F0218314002832")
init = u2.init.Initer(device loglevel=logging.INFO)
init.install()
#这里是卸载在手机上安装的app程序
def uninstall():
device = adbutils.adb.device("D5F0218314002832")
init = u2.init.Initer(device loglevel=logging.INFO)
init.uninstall()
#可以自己执行试一试
if __name__ == "__main__":
init()
uninstall()
这样就连接好了。下面介绍常用的方法
#通过USB, 假设设备序列是
import uiautomator2 as u2
d = u2.connect('D5F0218314002832')
d.app_install('http://some-domain.com/some.apk') #引号内为下载apk地址
d.app_start('com.eg.android.AlipayGphone') #引号内为包名称,这里为支付宝
d.app_stop('com.eg.android.AlipayGphone')
d.app_clear('com.eg.android.AlipayGphone')
# 停止所有
d.app_stop_all()
# 停止所有应用程序,除了com.examples.demo
d.app_stop_all(excludes=['com.examples.demo'])
d.disable_popups() # 自动跳过弹出窗口
d.disable_popups(False) # 禁用自动跳过弹出窗
#获取设备信息:
d.info
# 获取窗口大小
print(d.window_size())
# 设备垂直输出示例: (1080 1920)
# 设备水平输出示例: (1920 1080)
# 获取当前应用程序信息。对于某些android设备,输出可以为空
print(d.current_app())
#获取设备序列号
print(d.serial)
#获取WIFI IP
print(d.wlan_ip)
#获取详细的设备信息
print(d.device_info)
#关键事件:
d.screen_on()#打开屏幕
d.screen_off() #关闭屏幕
#获取当前屏幕状态
d.info.get('screenOn') # 需要 Android> = 4.4
#硬键盘和软键盘操作
d.press("home") # 点击home键
d.press("back") # 点击back键
d.press("left") # 点击左键
d.press("right") # 点击右键
d.press("up") # 点击上键
d.press("down") # 点击下键
d.press("center") # 点击选中
d.press("menu") # 点击menu按键
d.press("search") # 点击搜索按键
d.press("enter") # 点击enter键
d.press("delete") # 点击删除按键
d.press("recent") # 点击近期活动按键
d.press("volume_up") # 音量
d.press("volume_down") # 音量-
d.press("volume_mute") # 静音
d.press("camera") # 相机
d.press("power") #电源键
#解锁屏幕
d.unlock()
#手势与设备的交互:
# 单击屏幕
d.click(x y) # x y为点击坐标
# 双击屏幕
d.double_click(x y)
d.double_click(x y 0.1) # 默认两个单击之间间隔时间为0.1秒
# 长按
d.long_click(x y)
d.long_click(x y 0.5) # 长按0.5秒(默认)
# 滑动
d.swipe(sx sy ex ey)
d.swipe(sx sy ex ey 0.5) #滑动0.5s(default)
#拖动
d.drag(sx sy ex ey)
d.drag(sx sy ex ey 0.5)#拖动0.5s(default)
# 滑动点 多用于九宫格解锁,提前获取到每个点的相对坐标(这里支持百分比)
# 从点(x0 y0)滑到点(x1 y1)再滑到点(x2 y2)
# 两点之间的滑动速度是0.2秒
d.swipe((x0 y0) (x1 y1) (x2 y2) 0.2)
# 注意:单击,滑动,拖动操作支持百分比位置值。例:
d.long_click(0.5 0.5) 表示长按屏幕中心
#重点重点重点。。。开发使用中经常用到
# 检索方向
# 检索方向
# 检索方向
d.orientation
# 检索方向。输出可以是 "natural" or "left" or "right" or "upsidedown"
# 设置方向
d.set_orientation("l") # or "left"
d.set_orientation("r") # or "right"
d.set_orientation("n") # or "natural"
#冻结/ 开启旋转
d.freeze_rotation() # 冻结旋转
d.freeze_rotation(False) # 开启旋转
########## 截图 ############
# 截图并保存到电脑上的一个文件中,需要Android>=4.2。
d.screenshot("home.jpg")
# 得到PIL.Image格式的图像. 但你必须先安装pillow
image = d.screenshot() # default format="pillow"
image.save("home.jpg") # 或'home.jpg',目前只支.jpg 和 jpg格式的图像
# 得到OpenCV的格式图像。当然,你需要numpy和cv2安装第一个
import cv2
image = d.screenshot(format='opencv')
cv2.imwrite('home.jpg' image)
# 获取原始JPEG数据
imagebin = d.screenshot(format='raw')
open("some.jpg" "wb").write(imagebin)
#############################
# 转储UI层次结构
# get the UI hierarchy dump content (unicoded).(获取UI层次结构转储内容)
d.dump_hierarchy()
# 打开通知或快速设置
d.open_notification() #下拉打开通知栏
d.open_quick_settings() #下拉打开快速设置栏
# 检查特定的UI对象是否存在
d(text="Settings").exists # 返回布尔值,如果存在则为True,否则为False
d.exists(text="Settings") # 另一种写法
# 高级用法
d(text="Settings").exists(timeout=3) # 等待'Settings'在3秒钟出现
# 获取特定UI对象的信息
d(text="Settings").info
# 获取/设置/清除可编辑字段的文本(例如EditText小部件)
d(text="Settings").get_text() #得到文本小部件
d(text="Settings").set_text("My text...") #设置文本
d(text="Settings").clear_text() #清除文本
# 获取Widget中心点
d(text="Settings").center()
#d(text="Settings").center(offset=(0 0)) # 基准位置左前
#UI对象有五种定位方式:
# text、resourceId、description、className、xpath、坐标
# 执行单击UI对象
#text定位单击
d(text="Settings").click()
d(text="Settings" className="android.widget.TextView").click()
#resourceId定位单击
d(resourceId="com.ruguoapp.jike:id/tv_title" className="android.widget.TextView").click()
#description定位单击
d(description="设置").click()
d(description="设置" className="android.widget.TextView").click()
#className定位单击
d(className="android.widget.TextView").click()
#xpath定位单击
d.xpath("//android.widget.FrameLayout[@index='0']/android.widget.LinearLayout[@index='0']").click()
#坐标单击
d.click(182 1264)
# 等待元素出现(最多10秒),出现后单击
d(text="Settings").click(timeout=10)
# 在10秒时点击,默认的超时0
d(text='Skip').click_exists(timeout=10.0)
# 单击直到元素消失,返回布尔
d(text="Skip").click_gone(maxretry=10 interval=1.0) # maxretry默认值10 interval默认值1.0
# 点击基准位置偏移
d(text="Settings").click(offset=(0.5 0.5)) # 点击中心位置,同d(text="Settings").click()
d(text="Settings").click(offset=(0 0)) # 点击左前位置
d(text="Settings").click(offset=(1 1)) # 点击右下
# 执行双击UI对象
d(text="设置").double_click() # 双击特定ui对象的中心
d.double_click(x y 0.1) # 两次单击之间的默认持续时间为0.1秒
#执行长按UI对象
# 长按特定UI对象的中心
d(text="Settings").long_click()
d.long_click(x y 0.5) # 长按坐标位置0.5s默认
# 将UI对象拖向另一个点或另一个UI对象
# Android<4.3不能使用drag.
# 在0.5秒内将UI对象拖到屏幕点(x y)
d(text="Settings").drag_to(x y duration=0.5)
# 将UI对象拖到另一个UI对象的中心位置,时间为0.25秒
d(text="Settings").drag_to(text="Clock" duration=0.25)
有很多方法没有介绍到,在开发使用过程中可以点击出来看看。
像python代码中的安装、卸载等方法笔者也是看源码后找到的方法。在网上很多介绍中没有提及到这块的内容。