1.源码能够在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.pypython
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里面有这几个动做:android
1.有些九宫格的每一个点能够直接定位到,这种相对来讲容易一点,有一些九宫格就是一整个元素,好比QQ的九宫格解锁。web
2.解决问题思路:先获取元素坐标位置,再获取元素大小,而后切割图片,分别计算出每一个点的坐标app
# 定位九宫格元素 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.ui
1.解锁思路:先press按住第一个点,再wait等待,接着移动带第二个点,再wait,最后release释放手指,perform执行code
2.好比我要画出一个Z形状,依次通过的点1,2,3,5,7,8,9orm
# 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()
在学习过程当中有遇到疑问的,能够appium+python QQ群交流:330467341blog