appium+python手势密码

问题:uiautomator获取到的手势是一整块区域,无法获取到每个点。

方法:可以使用LockPatternView对象拿到左上角的坐标值

 原理:

 将九宫格分割为6块, 左上角顶部坐标为[660,277], 我们假设为【startX,startY】

 获取整个区域的高度为 height , 宽度为width ,  则Xstep = width / 6 = 100,   Ystep = height / 6 100. 其中Xstep和Ystep分别表示被分割为6块后,每一块的宽度和高度。

     根据上述的变量,我们可以推算出:

    第一个点的坐标为【startX+Xstep, startY + Ystep】= 【760,377】;  第二个点的坐标为【startX+3*Xstep, startY + Ystep】=【960,377】; 第三个点的坐标为【startX+5*Xstep,startY + Ystep】= 【1160,377】

    第四个点的坐标为【startX+Xstep, startY + 3*Ystep】= 【760,,577】;第五个点的坐标为【startX+3*Xstep, startY + 3*Ystep】=【960,577】; 第六个点的坐标为【startX+5*Xstep,startY + 3*Ystep】= 【1160,577】

    第七个点的坐标为【startX+Xstep, startY + 5*Ystep】= 【760,777】;第八个点的坐标为【startX+3*Xstep, startY + 5*Ystep】=【960,777】; 第九个点的坐标为【startX+5*Xstep,startY + 5*Ystep】= 【1160,777】

 运用TouchAction操作 appium API : TouchAction 操作

代码如下:

    def move_gesture_code(self):
        # 获取元素左上角坐标
        ele = self.find_element(*login_element.gesture_code_id).location
        x = ele.get('x')
        y = ele.get('y')

        # 获取元素宽、高
        ele_size = self.find_element(*login_element.gesture_code_id).size
        width = ele_size['width']
        height = ele_size['height']

        xstep = int(width/6)
        ystep = int(height/6)
        print(xstep)
        print(ystep)
        beginx = int(x + xstep)
        beginy = int(y + ystep)
        print(beginx)
# 手势是 1 2 3 6 9 self.touch_long_press(beginx, beginy, 1
000).move_to(x=beginx+2*xstep, y=beginy).wait(200).move_to(x=beginx+4*xstep, y=beginy) .move_to(x=beginx+4*xstep, y=beginy+2*ystep).move_to(x=beginx+4*xstep, y=beginy+4*ystep).perform().release()
find_element()和touch_long_press()为自己封装的方法
   from appium.webdriver.common.touch_action import TouchAction  # 需要导入 TouchAction

def touch_long_press(self, x0, y0, t0): # 长按 return TouchAction(self.driver).long_press(x=x0, y=y0, duration=t0)

 

 更简洁的方法:方便各个点的点击,用字典的方法

baseClass.py

from appium.webdriver.common.touch_action import 
-----------------------------------------------------------
from appium.webdriver.common.touch_action import TouchAction # 需要导入 TouchAction 
#
TouchAction操作

def touch_pree(self, x00, y00):
return TouchAction(self.driver).press(x=x00, y=y00)

def touch_long_press(self, x0, y0, t0): # 长按
return TouchAction(self.driver).long_press(x=x0, y=y0, duration=t0)
# 手势密码9点坐标
def point_nine(self, *loc):
# 获取元素坐标
ele = self.find_element(*loc).location
x
= ele.get('x')
y
= ele.get('y')
# 获取元素宽、高
ele_size = self.find_element(*loc).size
width
= ele_size['width']
height
= ele_size['height']
xstep
= int(width / 6)
ystep
= int(height / 6)
beginx
= int(x + xstep)
beginy
= int(y + ystep)

'''
1 2 3
4 5 6
7 8 9
'''
list_point
= {
"1": [beginx, beginy],
"2": [beginx+2*xstep, beginy],
"3": [beginx+4*xstep, beginy],
"4": [beginx, beginy + 2 * ystep],
"5": [beginx + 2 * xstep, beginy + 2 * ystep],
"6": [beginx + 4 * xstep, beginy + 2 * ystep],
"7": [beginx, beginy + 4 * ystep],
"8": [beginx + 2 * xstep, beginy + 4 * ystep],
"9": [beginx + 4 * xstep, beginy + 4 * ystep]
}

return list_point

login_page.py

from src.common import baseClass


class LoginPage(baseClass.BaseClass):  # 继承公共类
    def setting_gesture_code(self, n1, n2, n3, n4, n5):
        coo = self.point_nine(*login_element.gesture_code_id)  # 调用baseClass.py中的point_nine()获取9个点坐标
        # 1 2 3 6 9
        return self.touch_long_press(coo[n1][0], coo[n1][1], 1000).move_to(x=coo[n2][0], y=coo[n2][1]).wait(200).move_to(x=coo[n3][0], y=coo[n3][1])
            .move_to(x=coo[n4][0], y=coo[n4][1]).wait(200).move_to(x=coo[n5][0], y=coo[n5][1]).wait(200) 
            .release().perform().wait(500)

运用:

# 手势密码 1 2 3 6 9
 self.login_page.setting_gesture_code('1', '2', '3', '6', '9')

 设置密码时,有需要再次确认的,加个循环即可

原文地址:https://www.cnblogs.com/may18/p/14479039.html