python图案绘制解锁_Python&Appium实现安卓手机图形解锁

首先,在解锁状态下,建立一个Session,打开APP。然后,调用press_keycode()方法传入整型数值"26",锁定屏幕。通过implicitly_wait()方法等待两秒后,再次调用press_keycode()方法按下电源键,点亮屏幕。这时候看到的手机界面如下所示:

c20ae6f51567edece0f60e5933468d74.png

此时,我们需要调用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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值