Appium-实现-9-宫格划屏解锁(二)
2017-01-01 10:46
第一篇
研究了很久,终于稳定可靠实现了9宫格划屏解锁,现分享出来,给有需要人员使用
1.实现算法,首先获得9宫格外框,横、列分别分成3份,然后再找到哪个方格中的圆心位置
| w | w | w | h
|--------------------|
| | | |
| | | | h
|-------------------|
| | | | h
| | | |
public static Point[] getGesturePoints(WebElement lock_view) {
int start_x = lock_view.getLocation().x;
int start_y = lock_view.getLocation().y;
int viewWidth = lock_view.getSize().width;
int viewHeight = lock_view.getSize().height;
Point[] centerCxCy = new Point[9];
int w = viewWidth / 3;
int h = viewHeight / 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Point p = new Point((int) ((i * w) + w / 2 + start_x),
(int) ((j * h) + h / 2 + start_y));
centerCxCy[j * 3 + i] = p;
}
}
return centerCxCy;
}
2.密码转换为坐标点,将9宫格从上到下编号如下,然后将对应号的点做这密码点输入,之后再转化为对应的坐格点划动。
1 2 3
4 5 6
7 8 9
String password = "14789";
WebElement lockPatternView = driver.findElement(By.id("com.android.keyguard:id/lockPatternView"));
Point[] centerCxCy = getGesturePoints(lockPatternView);
Point[] points = new Point[password.length()];
for (int i = 0; i < password.length(); i++) {
int id = Integer.parseInt(password.substring(i, i+1));
points[i] = centerCxCy[id-1];
}
for (int i = 0; i < 2; i++) {
HandGesture(points);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
3.划动屏幕,加入等待时间,否则划动太快了,应用程序无法正常解锁
public static void HandGesture(Point[] points) {
TouchAction gesture = null;
if (points.length > 0) {
gesture = new TouchAction(driver).press(points[0].x,
points[0].y);
try {
gesture.waitAction(100);
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 1; i < points.length; i++) {
Point p = points[i];
gesture.moveTo(p.x, p.y);
try {
gesture.waitAction(100);
} catch (Exception e) {
e.printStackTrace();
}
}
gesture.release();
gesture.perform();
}
https://testerhome.com/topics/3008
第二篇
testerhome里看了很多前辈对九宫格手势密码的操作,也看到许多人在问,前段时间也刚做完app,也封装了一个手势你们操作,分享给大家,希望给大家一些思路我的这个是这样的,首先自己标记九宫格的排列顺序即索引(list里会用到),横三排数分别是0,1,2,3,4,5,6,7,8 对,就跟大家通过UIautomator获取到的索引一致,在设置或者解锁时,自行输入索引号即可(该索引号是可在手机上连滑成功的,而非类似01238 这样跳点滑),说那么多,看代码
#coding=utf-8
'''手势密码模块'''
import os,sys,time
sys.path.append(os.path.abspath('..'))
from appium.webdriver.common.touch_action import TouchAction
from test_control.app_mylog import logs
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from test_control.app_page_objectdata import pageobject
class GesturePwd(object):
"""手势密码操作"""
@classmethod
def gesture_pwd(cls,driver,gesture_pwd_index):
u"""
1.gesture_pwd_index 是手势密码点排列顺序
2.采用字符串拼接方式,根据密码点个数拼接,最后执行
"""
logs.info(u'手势密码操作')
bt =driver
WebDriverWait(bt,10).until(expected_conditions.presence_of_element_located((By.ID,pageobject['gesture'])))
pwd_point_list = bt.find_elements_by_class_name(pageobject['gesture_elements']) #获取9个密码点元素
touch_start = "TouchAction(bt).press(pwd_point_list[gesture_pwd_index[0]]).move_to(pwd_point_list[gesture_pwd_index[0]])"
a = touch_start
for i in gesture_pwd_index[1:-2]:
b = ".move_to(pwd_point_list[{}]).wait(100)".format(i)
a = a + b
final = "{0}.move_to(pwd_point_list[gesture_pwd_index[{1}]]).release().perform()".format(a,-1)
exec(final)
logs.info(u'手势密码操作完成')
来自:https://testerhome.com/topics/6878