appium+python自动化33-解锁九宫格(TouchAction)
一、TouchAction
1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py
```
class TouchAction(object):
def __init__(self, driver=None):
self._driver = driver
self._actions = []
def tap(self, element=None, x=None, y=None, count=1):
模拟手指触摸屏
def press(self, el=None, x=None, y=None):
短按:模拟手指按住一个元素,或者坐标
def long_press(self, el=None, x=None, y=None, duration=1000):
长按:模拟按住一个元素,或者坐标
def wait(self, ms=0):
按住元素后的等待时间
def move_to(self, el=None, x=None, y=None):
移动手指到另外一个元素,或者坐标,注意这里坐标不是绝对坐标,是偏移量
def release(self):
释放手指
def perform(self):
执行前面的动作
```
2.TouchAction里面有这几个动作:
- 触摸 (tap)
- 短按 (press)
- 长按 (long_press)
- 等待 (wait)
- 移动到 (moveTo)
- 释放 (release)
- 执行 (perform)
二、九宫格解锁
1.有些九宫格的每个点可以直接定位到,这种相对来说容易一点,有一些九宫格就是一整个元素,比如QQ的九宫格解锁。
2.解决问题思路:先获取元素坐标位置,再获取元素大小,然后切割图片,分别计算出每个点的坐标
```
# 定位九宫格元素
jiu = 'resourceId("com.tencent.mobileqq:id/name").index(6)'
loc = driver.find_element_by_android_uiautomator(jiu).location
print("获取九宫格坐标位置:%s"%loc)
s = driver.find_element_by_android_uiautomator(jiu).size
print("获取九宫格宽和高:%s"%s)
```
3.给每个圆圈编号从左到右1,2,3依次第二行4,5,6第三行7,8,9
```
gongge = {}
gongge[1] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6)
gongge[2] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6)
gongge[3] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6)
gongge[4] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6*3)
gongge[5] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6*3)
gongge[6] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6*3)
gongge[7] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6*5)
gongge[8] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6*5)
gongge[9] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6*5)
print gongge
```
三、 偏移量
1.这里有个坑,press里面的参数是元素的坐标位置,但是move_to里面的是相对于前面一个元素的偏移位置。所以需要单独写一个函数,计算偏移量。
```
def pianyi(a=1,b=2):
'''计算从a点到b点的偏移量'''
g1 = gongge[a]
g2 = gongge[b]
r = (None, g2[1]-g1[1], g2[2]-g1[2])
return r
```
2.另外press和move_to里面都有三个参数,第一个参数默认为None,所以我返回的参数里面第一个写None.
四、移动手指
1.解锁思路:先press按住第一个点,再wait等待,接着移动带第二个点,再wait,最后release释放手指,perform执行
2.比如我要画出一个Z形状,依次经过的点1,2,3,5,7,8,9
五、 参考代码
```
# coding:utf-8
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from time import sleep
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '4.4.2',
'appPackage': 'com.tencent.mobileqq',
'appActivity': 'com.tencent.mobileqq.activity.SplashActivity',
'noReset': "true"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(5)
jiu = 'resourceId("com.tencent.mobileqq:id/name").index(6)'
loc = driver.find_element_by_android_uiautomator(jiu).location
print("获取九宫格坐标位置:%s"%loc)
s = driver.find_element_by_android_uiautomator(jiu).size
print("获取九宫格宽和高:%s"%s)
# 获取九个点的坐标
gongge = {}
gongge[1] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6)
gongge[2] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6)
gongge[3] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6)
gongge[4] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6*3)
gongge[5] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6*3)
gongge[6] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6*3)
gongge[7] = (None, loc["x"]+s["width"]/6, loc["y"]+s["height"]/6*5)
gongge[8] = (None, loc["x"]+s["width"]/6*3, loc["y"]+s["height"]/6*5)
gongge[9] = (None, loc["x"]+s["width"]/6*5, loc["y"]+s["height"]/6*5)
print gongge
def pianyi(a=1,b=2):
'''计算从a点到b点的偏移量'''
g1 = gongge[a]
g2 = gongge[b]
r = (None, g2[1]-g1[1], g2[2]-g1[2])
return r
# 执行解锁
TouchAction(driver).press(*gongge[1]).wait(300).move_to(*pianyi(1,2)).wait(300).move_to(*pianyi(2,3)).wait(
300).move_to(*pianyi(3,5)).wait(300).move_to(*pianyi(5,7)).wait(300).move_to(*pianyi(7,8)).wait(300).move_to(*pianyi(8,9)).wait(
300).release().perform()
```