1. 程式人生 > >Appium解鎖九宮格(TouchAction)

Appium解鎖九宮格(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): 移動手指到另外一個元素,或者座標,appium-desk1.6之後改用的是絕對座標!!!
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。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

參考程式碼

# 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()