# -*- coding: utf-8 -*-
import re
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
from appium import webdriver
"""
测试前请打开开发者选项的“充电时不锁屏屏幕”
"""
class SudoUnlock(object):
def __init__(self, android_version, package_name, activity_name):
"""
:param android_version: 安卓版本
:param package_name: 启动包名
:param activity_name: 启动Activity名
"""
desired_caps = {
'platformName': 'Android', # 平台名称
'deviceName': 'test', # 设备名称(任写)
'platformVersion': android_version, # 安卓版本
'appPackage': package_name, # 启动包名
'appActivity': activity_name, # 启动 Acclivity
'noReset': True, # 重置(不会保留之前的启动数据)
'newCommandTimeout': 60000 # 超时时间(一分钟)
}
if android_version == "11":
# Android 11 添加此参数
desired_caps["automationName"] = "uiautomator2"
# 开始初始化
print("Appium 正在初始化...")
# 启动服务/通过4723端口来建立一个会话
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 隐式等待15s
self.driver.implicitly_wait(15)
print("Appium 初始化完成...")
# 进入锁屏界面
def to_unlock(self):
self.driver.keyevent(3)
WebDriverWait(self.driver, 5000).until(
lambda x: x.find_element_by_android_uiautomator('new UiSelector().text("%s")' % "电话"))
self.driver.keyevent(26)
sleep(0.1)
self.driver.keyevent(26)
WebDriverWait(self.driver, 5000).until(
lambda x: x.find_element_by_android_uiautomator(
'new UiSelector().resourceId("%s")' % "com.android.systemui:id/default_clock_view"))
# 获取屏幕宽度
x = self.driver.get_window_size()['width']
# 获取屏幕高度
y = self.driver.get_window_size()['height']
self.driver.swipe(x / 2, 3 * y / 4, x / 2, y / 4)
WebDriverWait(self.driver, 5000).until(
lambda x: x.find_element_by_android_uiautomator(
'new UiSelector().resourceId("%s")' % "com.android.systemui:id/lockPatternView"))
# 获取九宫格每个点的坐标
def get_sudoku_point(self):
# 获取九宫格元素
sudokuUnlock_element = self.driver.find_element_by_android_uiautomator(
'new UiSelector().resourceId("%s")' % "com.android.systemui:id/lockPatternView")
# 此位置为九宫格左上角坐标
# {'x': 108, 'y': 1329}
sudokuUnlock_start_x = sudokuUnlock_element.location.get("x")
sudokuUnlock_start_y = sudokuUnlock_element.location.get("y")
# 九宫格宽和高
sudokuUnlock_height = sudokuUnlock_element.size.get("height") # 864
sudokuUnlock_width = sudokuUnlock_element.size.get("width") # 864
# 中心点坐标
center_point_x = sudokuUnlock_start_x + sudokuUnlock_width / 2
center_point_y = sudokuUnlock_start_y + sudokuUnlock_height / 2
# 每行每列平分6份
p_1 = (sudokuUnlock_start_x + sudokuUnlock_height / 6, sudokuUnlock_start_y + sudokuUnlock_width / 6)
p_2 = (sudokuUnlock_start_x + sudokuUnlock_height * 3 / 6, sudokuUnlock_start_y + sudokuUnlock_width / 6)
p_3 = (sudokuUnlock_start_x + sudokuUnlock_height * 5 / 6, sudokuUnlock_start_y + sudokuUnlock_width / 6)
p_4 = (sudokuUnlock_start_x + sudokuUnlock_height / 6, sudokuUnlock_start_y + sudokuUnlock_width * 3 / 6)
p_5 = (sudokuUnlock_start_x + sudokuUnlock_height * 3 / 6, sudokuUnlock_start_y + sudokuUnlock_width * 3 / 6)
p_6 = (sudokuUnlock_start_x + sudokuUnlock_height * 5 / 6, sudokuUnlock_start_y + sudokuUnlock_width * 3 / 6)
p_7 = (sudokuUnlock_start_x + sudokuUnlock_height / 6, sudokuUnlock_start_y + sudokuUnlock_width * 5 / 6)
p_8 = (sudokuUnlock_start_x + sudokuUnlock_height * 3 / 6, sudokuUnlock_start_y + sudokuUnlock_width * 5 / 6)
p_9 = (sudokuUnlock_start_x + sudokuUnlock_height * 5 / 6, sudokuUnlock_start_y + sudokuUnlock_width * 5 / 6)
# 存入列表
p_list = [p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8, p_9]
return p_list
# 输入4-9位数字
def input_sudoku_nums(self):
# 输入4-9位数字来解锁九宫格
sudoku_nums = None
# 输入次数
input_flag = 5
while input_flag:
# 是有重复
is_repetition = True
sudoku_nums = input("请输入4-9位九宫格解锁数字:")
# 判断有没有重复
s_list = list(sudoku_nums)
for i in range(len(s_list)):
# 有重复的数字
if sudoku_nums.count(s_list[i]) != 1:
is_repetition = False
break
# 判断4-9位的数字
if re.compile(r"^[1-9]{4,9}$").findall(sudoku_nums) and is_repetition:
break
else:
input_flag = input_flag - 1
if input_flag == 0:
raise TypeError("没救了,告辞!")
print(f"您的输入有误,请重新输入!您还有【{input_flag}】次输入机会!")
return sudoku_nums
# 将输入数字转换为九宫格坐标
def nums_to_point(self):
# 输入的数字
input_nums = self.input_sudoku_nums()
# 坐标点的集合
p_list = self.get_sudoku_point()
# 存放转化后的坐标点
input_nums_point_list = []
# 遍历每一个元素
for num in input_nums:
# 坐标点
x = p_list[int(num) - 1][0]
y = p_list[int(num) - 1][1]
tmp_tuple = (x, y)
input_nums_point_list.append(tmp_tuple)
# print(f"输入数字【{input_nums}】转化为坐标集合:{input_nums_point_list}")
return input_nums_point_list
# 九宫格解锁压测
def sudoko_stress_test(self, times):
# 获取actions
actions = TouchAction(self.driver)
# 待滑动的坐标点集合
points_list = self.nums_to_point()
# 坐标点个数
len_points_list = len(points_list)
for i in range(times):
if len_points_list == 4:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).release().perform()
elif len_points_list == 5:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).move_to(
x=points_list[4][0], y=points_list[4][1]).release().perform()
elif len_points_list == 6:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).move_to(
x=points_list[4][0], y=points_list[4][1]).move_to(
x=points_list[5][0], y=points_list[5][1]).release().perform()
elif len_points_list == 7:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).move_to(
x=points_list[4][0], y=points_list[4][1]).move_to(
x=points_list[5][0], y=points_list[5][1]).move_to(
x=points_list[6][0], y=points_list[6][1]).release().perform()
elif len_points_list == 8:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).move_to(
x=points_list[4][0], y=points_list[4][1]).move_to(
x=points_list[5][0], y=points_list[5][1]).move_to(
x=points_list[6][0], y=points_list[6][1]).move_to(
x=points_list[7][0], y=points_list[7][1]).release().perform()
elif len_points_list == 9:
actions.press(
x=points_list[0][0], y=points_list[0][1]).move_to(
x=points_list[1][0], y=points_list[1][1]).move_to(
x=points_list[2][0], y=points_list[2][1]).move_to(
x=points_list[3][0], y=points_list[3][1]).move_to(
x=points_list[4][0], y=points_list[4][1]).move_to(
x=points_list[5][0], y=points_list[5][1]).move_to(
x=points_list[6][0], y=points_list[6][1]).move_to(
x=points_list[7][0], y=points_list[7][1]).move_to(
x=points_list[8][0], y=points_list[8][1]).release().perform()
sleep(1)
if "电话" in self.driver.page_source:
print(f"第【{i+1}】次解锁成功")
self.to_unlock()
else:
print(f"第【{i+1}】次解锁失败!!!")
break
# for l in points_list:
# x = l[0]
# y = l[1]
# actions.press(x=x, y=y).move_to()
if __name__ == '__main__':
sudoku = SudoUnlock("11", "com.android.settings", "com.android.settings.Settings")
sudoku.to_unlock()
sudoku.get_sudoku_point()
# 压测10次
sudoku.sudoko_stress_test(10)
网友评论