首先,在解鎖狀態(tài)下,建立一個(gè)Session,打開(kāi)APP。然后,調(diào)用press_keycode()方法傳入整型數(shù)值"26",鎖定屏幕。通過(guò)implicitly_wait()方法等待兩秒后,再次調(diào)用press_keycode()方法按下電源鍵,點(diǎn)亮屏幕。這時(shí)候看到的手機(jī)界面如下所示:
此時(shí),我們需要調(diào)用login_unlock()方法繪制圖案解鎖手機(jī)(預(yù)先設(shè)置好的解鎖圖形如上圖所示)。
login_unlock()方法的詳細(xì)介紹如下:
1、通過(guò)find_element_by_id()方法找到九宮格的View。
lock_pattern = self.driver.find_element_by_id("com.android.keyguard:id/lockPatternView")
2、通過(guò)lock_pattern變量獲取View的初始坐標(biāo)值和寬度高度。
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、通過(guò)寬度計(jì)算偏移量。
offset = width / 6
4、通過(guò)偏移量計(jì)算九宮格內(nèi)九個(gè)點(diǎn)各自的x,y坐標(biāo)值。
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、計(jì)算從當(dāng)前點(diǎn)移動(dòng)到下一個(gè)點(diǎn)的偏移量。
p3 = p13[0] - p11[0]
6、執(zhí)行移動(dòng)操作。
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) # 調(diào)用解鎖的方法
self.login_unlock() # 解鎖
def login_unlock(self):
# 通過(guò)ID找到九宮格的View
lock_pattern = self.driver.find_element_by_id("com.android.keyguard:id/lockPatternView") # 獲取View的x,y坐標(biāo)值
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
# 計(jì)算九宮格內(nèi)九個(gè)點(diǎn)的x,y坐標(biāo)值
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) # 計(jì)算移動(dòng)到下一個(gè)點(diǎn)的偏移量
p3 = p13[0] - p11[0]
sleep(3) # 執(zhí)行移動(dòng)操作
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)