Appium Appium 九宫格 手势解锁

玄月指光 · August 14, 2017 · Last by xiaokang replied at September 06, 2018 · 3247 hits
本帖已被设为精华帖!

Appium 九宫格手势解锁

使用 Python 来解决

分析九宫格定位

整个九宫格是一个 view

九宫格图片

self.driver.find_element_by_id("com.elc:id/gesturepwd_create_lockview")

屏幕大小 x 720, y 1280

九宫格定位 x 80, y 488, width 640 height 1048

计算出大小 x 80 y 488

width = 640-80 width 等于 560
height = 1048-488 height 等于 560
九宫格是正方形

计算出第一个坐标 为了好计算,就把坐标的值全部用整数来计算

p11 = int(x + width / 6), int(y + height / 6) 
    (173, 581)

计算所有的坐标值

定位方式存在一个字典中

a = {
    "解锁提示": "com.elc:id/gesturepwd_create_text",
    "九宫格": "com.elc:id/gesturepwd_create_lockview",
    "继续": "com.elc:id/right_btn",
}
lock_pattern = self.driver.find_element_by_id(a["九宫格"])
x = lock_pattern.location.get('x')
y = lock_pattern.location.get('y')
width = lock_pattern.size.get('width')
height = lock_pattern.size.get('height')
print(x, y, width, height)
offset = width / 6
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)
print(p11, p12, p13)
print(p21, p22, p23)
print(p31, p32, p33)

80 488 560 560 # 九宫格坐标值 与上面计算的一样
(173, 581) (360, 581) (546, 581)
(173, 768) (360, 768) (546, 768)
(173, 954) (360, 954) (546, 954)

计算滑动偏移量 (做的效果图是 7)

p3 = p13[0] - p11[0] # 偏移的值是 373

滑动方法 TouchAction

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()

说明 滑动方法

# 横向滑动
从 173 滑动到 546 坐标的偏移量是 546-173 = 373
第一个值不变 press(x=p11[0], y=p11[1]).move_to(x=373, y =0),因为 y 的值是相同的,就不变
# 纵向滑动
move_to(x=0, y=373) 因为 x 的值不变,移动 y 的值
九宫格是正方的形,所以 从第 1 点到 3 点是 373, 从 3 点到 9 点也是 373

九宫格解锁完毕

这样的写法,不需要固定坐标值,根据不同的设备屏幕大小自动获取到 -- 偏移值

我遇到的坑-- 之前一直以为 TouchAction 的 move_to 是从 x y 坐标移动到另一个坐标,而不是偏移量,没有理解清楚,所以这里掉坑里了

最后附上完整的函数

def login_unlock(self):
       a = {
           "解锁提示": "com.elc:id/gesturepwd_create_text",
           "九宫格": "com.elc:id/gesturepwd_create_lockview",
           "继续": "com.elc:id/right_btn",
       }
       unlock_text = self.driver.find_element_by_id(a["解锁提示"])
       lock_pattern = self.driver.find_element_by_id(a["九宫格"])
       x = lock_pattern.location.get('x')
       y = lock_pattern.location.get('y')
       width = lock_pattern.size.get('width')
       height = lock_pattern.size.get('height')
       print(unlock_text.text)
       print(x, y, width, height)
       offset = width / 6 
       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()

效果视频

Vimeo

共收到 11 条回复 时间 点赞
思寒_seveniruby 将本帖设为了精华贴 15 Aug 15:18
匿名 #3 · August 16, 2017

可惜 iphone 底层不支持

效果视频看不到,能否把 Vimeo 视频地址发一下?

这个移动的是偏移量真的很坑,函数提示里边只有:Move the pointer from the previous point to the element or point specified(将指针从上一个点移动到指定的元素或点),最后在官方 github 帮助文档上找到是移动偏移量。

lbw 回复

是的,我周末花了一天的时间来算这个偏移量,计算不同的屏幕的坐标点

陈恒捷 回复

视频正在上传到优酷视频,等待审核

玄月指光 回复

坐等楼主视频!谢谢了

楼主好,我根据你的代码用在我的测试上,可以一直报错,麻烦帮忙看一下,我该怎么改

感谢楼主,但是我的没有解锁,也没有报错,什么情况呢???
我是用按步骤来一段一段偏移。

执行,成功。

simple 专栏文章:[精华帖] 社区历年精华帖分类归总 中提及了此贴 13 Dec 20:49
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up