Python&Appium實現安卓手機圖形解鎖!

首先,在解鎖狀態下,建立一個Session,打開APP。然後,調用press_keycode()方法傳入整型數值"26",鎖定屏幕。通過implicitly_wait()方法等待兩秒後,再次調用press_keycode()方法按下電源鍵,點亮屏幕。這時候看到的手機界面如下所示:

Python&Appium實現安卓手機圖形解鎖!

此時,我們需要調用login_unlock()方法繪製圖案解鎖手機(預先設置好的解鎖圖形如上圖所示)。

login_unlock()方法的詳細介紹如下:

1、通過find_element_by_id()方法找到九宮格的View。

lock_pattern = self.driver.find_element_by_id("com.android.keyguard:id/lockPatternView")

2、通過lock_pattern變量獲取View的初始座標值和寬度高度。

x = lock_pattern.location.get('x')y = lock_pattern.location.get('y')width = lock_pattern.size.get('width')height = lock_pattern.size.get('height')

3、通過寬度計算偏移量。

offset = width / 6

4、通過偏移量計算九宮格內九個點各自的x,y座標值。

p11 = int(x + width / 6), int(y + height / 6)p12 = int(x + width / 2), int(y + height / 6)p13 = int(x + width - offset), int(y + height / 6)p21 = int(x + width / 6), int(y + height / 2)p22 = int(x + width / 2), int(y + height / 2)p23 = int(x + width - offset), int(y + height / 2)p31 = int(x + width / 6), int(y + height - offset)p32 = int(x + width / 2), int(y + height - offset)p33 = int(x + width - offset), int(y + height - offset)

5、計算從當前點移動到下一個點的偏移量。

p3 = p13[0] - p11[0]

6、執行移動操作。

TouchAction(self.driver).press(x=p11[0], y=p11[1]).move_to(x=p3, y=0).wait(1000).move_to(x=0, y=p3).wait(1000).release().perform()

完整代碼如下:

import unittestfrom appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom time import sleep# 圖形解鎖class unlockTest(unittest.TestCase): def test_unlock(self): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4.4' desired_caps['app'] = '/Users/a140/Downloads/test.apk' desired_caps['deviceName'] = '03083025d0250909' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 按電源鍵,鎖屏 self.driver.press_keycode(26) self.driver.implicitly_wait(2) # 按電源鍵,解鎖 self.driver.press_keycode(26) # 調用解鎖的方法 self.login_unlock() # 解鎖 def login_unlock(self): # 通過ID找到九宮格的View lock_pattern = self.driver.find_element_by_id("com.android.keyguard:id/lockPatternView") # 獲取View的x,y座標值 x = lock_pattern.location.get('x') y = lock_pattern.location.get('y') # 獲取View的寬度和高度 width = lock_pattern.size.get('width') height = lock_pattern.size.get('height') # 偏移量 offset = width / 6 # 計算九宮格內九個點的x,y座標值 p11 = int(x + width / 6), int(y + height / 6) p12 = int(x + width / 2), int(y + height / 6) p13 = int(x + width - offset), int(y + height / 6) p21 = int(x + width / 6), int(y + height / 2) p22 = int(x + width / 2), int(y + height / 2) p23 = int(x + width - offset), int(y + height / 2) p31 = int(x + width / 6), int(y + height - offset) p32 = int(x + width / 2), int(y + height - offset) p33 = int(x + width - offset), int(y + height - offset) # 計算移動到下一個點的偏移量 p3 = p13[0] - p11[0] sleep(3) # 執行移動操作 TouchAction(self.driver).press(x=p11[0], y=p11[1]).move_to(x=p3, y=0).wait(1000).move_to(x=0, y=p3).wait( 1000).release().perform()if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(unlockTest) unittest.TextTestRunner(verbosity=2).run(suite) 


分享到:


相關文章: