第一篇
研究了很久,終于穩(wěn)定可靠實(shí)現(xiàn)了9宮格劃屏解鎖,現(xiàn)分享出來(lái),給有需要人員使用
1.實(shí)現(xiàn)算法,首先獲得9宮格外框,橫、列分別分成3份,然后再找到哪個(gè)方格中的圓心位置
| 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.密碼轉(zhuǎn)換為坐標(biāo)點(diǎn),將9宮格從上到下編號(hào)如下,然后將對(duì)應(yīng)號(hào)的點(diǎn)做這密碼點(diǎn)輸入,之后再轉(zhuǎn)化為對(duì)應(yīng)的坐格點(diǎn)劃動(dòng)。
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.劃動(dòng)屏幕,加入等待時(shí)間,否則劃動(dòng)太快了,應(yīng)用程序無(wú)法正常解鎖
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里看了很多前輩對(duì)九宮格手勢(shì)密碼的操作,也看到許多人在問(wèn),前段時(shí)間也剛做完app,也封裝了一個(gè)手勢(shì)你們操作,分享給大家,希望給大家一些思路我的這個(gè)是這樣的,首先自己標(biāo)記九宮格的排列順序即索引(list里會(huì)用到),橫三排數(shù)分別是0,1,2,3,4,5,6,7,8 對(duì),就跟大家通過(guò)UIautomator獲取到的索引一致,在設(shè)置或者解鎖時(shí),自行輸入索引號(hào)即可(該索引號(hào)是可在手機(jī)上連滑成功的,而非類(lèi)似01238 這樣跳點(diǎn)滑),說(shuō)那么多,看代碼
#coding=utf-8
'''手勢(shì)密碼模塊'''
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):
"""手勢(shì)密碼操作"""
@classmethod
def gesture_pwd(cls,driver,gesture_pwd_index):
u"""
1.gesture_pwd_index 是手勢(shì)密碼點(diǎn)排列順序
2.采用字符串拼接方式,根據(jù)密碼點(diǎn)個(gè)數(shù)拼接,最后執(zhí)行
"""
logs.info(u'手勢(shì)密碼操作')
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個(gè)密碼點(diǎn)元素
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'手勢(shì)密碼操作完成')