Appium解锁九宫格(TouchAction)

【Appium解锁九宫格(TouchAction)】五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述Appium解锁九宫格(TouchAction)相关的知识,希望能为你提供帮助。
TouchAction1.源码可以在这个路径找到:Libsite-packagesappiumwebdrivercommon ouch_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): 移动手指到另外一个元素,或者坐标,appium-desk1.6之后改用的是绝对坐标!!!
def release(self): 释放手指def perform(self): 执行前面的动作

2.TouchAction里面有这几个动作:
  • 触摸 (tap)
  • 短按 (press)
  • 长按 (long_press)
  • 等待 (wait)
  • 移动到 (moveTo)
  • 释放 (release)
  • 执行 (perform)
九宫格解锁1.有些九宫格的每个点可以直接定位到,这种相对来说容易一点,有一些九宫格就是一整个元素,比如QQ的九宫格解锁。
Appium解锁九宫格(TouchAction)

文章图片

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。press和move_to里面都有三个参数,第一个参数默认为None,所以我返回的参数里面第一个写None.
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按住第一个点,再release手指,接着移动带第二个点,再release手指,最后perform执行
2.比如我要画出一个Z形状,依次经过的点1,2,3,5,7,8,9
Appium解锁九宫格(TouchAction)

文章图片

参考代码
# 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# 执行解锁 TouchAction(driver).press(*gongge[1]).wait(300).move_to(*gongge[2]).wait(300).move_to(*gongge[3]).wait(300).

move_to(*gongge[5]).wait(300).move_to(*gongge[7]).wait(300).move_to(*gongge[9]).wait(300).release().perform()




    推荐阅读