Touchaction
1. Source code can be found in this path: 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): 执行前面的动作
There are several actions in the 2.TouchAction:
- Touch (TAP)
- Short push (press)
- Long Press (long_press)
- Waiting (Wait)
- Move to (MoveTo)
- Free (release)
- Execution (perform)
Nine Gongge unlock
1. Some nine Gongge each point can be directly located, this relatively easy point, some nine Gongge is a whole element, such as QQ nine Gongge unlock.
2. Problem-Solving ideas: Get the coordinates of the element first, then get the element size, then cut the picture, calculate the coordinates of each point separately
# 定位九宫格元素jiu = 'resourceId("com.tencent.mobileqq:id/name").index(6)'loc = driver.find_element_by_android_uiautomator(jiu).locationprint("获取九宫格坐标位置:%s"%loc)s = driver.find_element_by_android_uiautomator(jiu).sizeprint("获取九宫格宽和高:%s"%s)
3. Give each circle number from left to right 4,5,6 the third row in the second row 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
Offset amount
1. There is a hole in the press, the parameter is the coordinate position of the element, but the move_to inside is relative to the previous element offset position. So you need to write a function separately to calculate the offset.
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. In addition the press and move_to inside have three parameters, the first parameter defaults to none, so I returned the parameter inside the first write none.
Move your finger
1. Unlocking ideas: Press and hold the first point, then wait, then move with the second point, wait, finally release released the Finger, perform execution
2. For example, I want to draw a Z-shape, followed by the point 1,2,3,5,7,8,9
Reference Code
# coding:utf-8from Appium Import webdriverfrom appium.webdriver.common.touch_action import touchactionfrom time Import Sleepdesired_caps = {' PlatformName ': ' Android ', ' devicename ': ' 127.0.0.1:62001 ', ' Platformversion ': ' 4.4.2 ', ' apppackage ': ' com.tencent.mobileqq ', ' appactivity ': ' com.t Encent.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). Locationprint ("Get nine Gongge coordinates location:%s"%loc) s = driver.find_ Element_by_android_uiautomator (JIU). Sizeprint ("Get nine Gangkan and High:%s"%s) # gets the coordinates of nine points 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 Gonggedef Pianyi (a=1,b=2): ' Calculate offset from point A to point B ' g1 = gongge[a] G2 = Go Ngge[b] r = (None, g2[1]-g1[1], g2[2]-g1[2]) return r# perform unlock touchaction (driver). Press (*gongge[1]). Wait (*). MOVE_TO (* Pianyi). Wait (+) move_to (*pianyi (2,3)). Wait (move_to (*pianyi)). Wait (+). 3,5 (Move_to (*pianyi)). Wait (+). Move_to (*pianyi (7,8)). Wait (+) move_to (*pianyi (8,9)). Wait (+). Release (). Perform ()
Appium+python Automation 33-Jiang Gongge (touchaction)