Python&Appium实现安卓手机图形解锁

发表于:2018-6-05 13:25

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Shengjie    来源:博客园

  首先,在解锁状态下,建立一个Session,打开APP。然后,调用press_keycode()方法传入整型数值"26",锁定屏幕。通过implicitly_wait()方法等待两秒后,再次调用press_keycode()方法按下电源键,点亮屏幕。这时候看到的手机界面如下所示:
  此时,我们需要调用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 unittest
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from 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)

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号