【UIautomator2 安卓自动化】教程

一、环境准备:

【安装】

安装uiautomator2

                    pip install --pre uiautomator2

                    pip install pillow

【初始化】

部署相关的守护进程。

电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

安装手机端控制程序

                    python -m uiautomator2 init

【连接】

wifi链接方法:

手机和电脑必须处于同一个网段才可使用wifi连接 

获取手机ip地址,可以在手机-设置-更多-关于手机 或者 打开手机wifi设置,长按设置wifi获取

import uiautomator2 as u2

d = u2.connect('192.168.31.234')

print(d.info)


定位元素

weditor定位元素的方法

安装:pip install --pre --upgrade weditor

cmd窗口中使用:

使用前需要在手机端安装 python -m uiautomator2 init,以保证链接正常

使用:python -m weditor


设置好连接方式后,点击connect 出现绿色叶子图案,及是连接成功,dump刷新页面



二、脚本操作

https://www.jianshu.com/p/874e5ceb72bb        

连接

d=u2.connect_wifi("ip")   #通过WiFi

d=u2.connect('id')  # devices id  通过设备名称

d=u2.connect_usb() # 通过USB

d.app_install(' url ')# 安装应用

d.disable_popups() #自动跳过弹出窗口

d.disable_popups(假)#禁用自动跳过弹出窗口

d.info   #获取基本信息

操作屏幕

d.windows.size()  #获取窗口大小

d.screen_on()#打开屏幕

d.screen_off()#关闭屏幕

d.info.get(' screenOn ')#获取当前屏幕状态

d.unlock()  #解锁屏幕

d.click(x,y)#点击屏幕坐标

d.double_click(x,y)#双击

d.long_click(x,y) # 长按一下屏幕

d.long_click(X,Y,1)#长按1秒(默认)

d.swipe(x, y, x, y)  #滑动

解锁9宫格,points手指不抬起
d.swipe_points([(0.228, 0.525),(0.005, 0.525),(0.766, 0.525),(0.5, 0.677),(0.228, 0.828),(0.497, 0.828),(0.761, 0.828)],0.05)

d.press("home")

d.press("back")

d.press(x, y)  # 按键操作

滑动相关

d(scrollable=True).scroll.vert.backward() # 向上滑动

d(scrollable=True).scroll.toEnd(60) # 竖直滑动到结尾

d(scrollable=True).scroll(steps=30) # 向下滑动

d(scrollable=True).scroll.toBeginning(steps=60) #竖直滑动到开头

d(scrollable=True).scroll.horiz.forward(steps=60) #水平向右滑动

d(scrollable=True).scroll.horiz.toEnd(steps=50, max_swipes=500) #水平向右滑动,直到最右边

d(scrollable=True).scroll.horiz.backward(steps=50) # 水平向左滑动,直到最左边

d(text="Settings").drag_to(x, y, duration=0.5) # 拖动

d(text="Settings").drag_to(text="text", duration=1) # 拖动到指定位置,1s内完成

d().gesture((a,b),(c,d), a1,b1),(c1,d1),steps=100) # 双指从(a,b),(c,d)滑动(a1,b1),(c1,d1),步长100

d().pinch_out(percent=33, steps=100) # 从屏幕外侧向中心滑动,percent为左右起始位置占两边的比例

d().pinch_out(percent=33, steps=100) # 从屏幕中心向外侧滑动,percent为左右起始位置占两边的比例

控件选择器

 
#选择器支持以下参数
# text,textContains,textMatches,textStartsWith
# className, classNameMatches
# description,descriptionContains,descriptionMatches,descriptionStartsWith
# checkable,checked,clickable,longClickable
# scrollable,enabled,focusable,focused,selected
# packageName, packageNameMatches
# resourceId, resourceIdMatches
# index, instance

d(text="Settings").clear_text()  # clear the text(清除文本信息)
d(text="Settings").set_text("My text...")  # set the text(设置文本信息)
d(text="Settings").click()
d(text="Settings").long_click()
#d(text="Settings").drag.to(x, y, steps=100)
d(text="Settings").drag.to(text="Clock", steps=50)
d(text="Settings").swipe.right()
d(text="Settings").swipe.left(steps=10)
d(text="Settings").swipe.up(steps=10)
d(text="Settings").swipe.down()
# d(text="Settings").gesture((sx1, sy1), (sx2, sy2)).to((ex1, ey1), (ex2, ey2))
# d().gestureM((sx1, sy1), (sx2, sy2),(sx3, sy3)).to((ex1, ey1), (ex2, ey2),(ex3,ey3))
d(text="Settings").wait.exists(timeout=3000)
d(text="Settings").exists(timeout=3000)
d(text="Settings").wait.gone(timeout=1000)

猜你喜欢

转载自www.cnblogs.com/Ray-2019/p/12186621.html