airtest环境安装
参考:
1
2
# 如果已经安装了WebAgentDriver了就不用再搞这个
git clone https://github.com/AirtestProject/iOS-Tagent.git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- encoding=utf8 -*-
__author__ = "xxxxx"
# 基础引入
from airtest.core.api import *
# 图片截取
from airtest.aircv import aircv
# 图片识别对比
from PIL import Image
import cv2
import numpy as np
# 解析plist
import plistlib
import os
def get_app_info(app_path):
plist_path = os.path.join(app_path, 'Info.plist')
# 检查 Info.plist 是否存在
if not os.path.exists(plist_path):
raise FileNotFoundError(f"{plist_path} not found.")
try:
# 读取 plist 文件
with open(plist_path, 'rb') as file:
plist = plistlib.load(file)
# logging.error(f"plist: {plist}")
# 提取 bundleId 和 app 名称
bundle_id = plist.get('CFBundleIdentifier', 'Unknown') # 默认值 'Unknown'
app_name = plist.get('CFBundleName', 'Unknown') # 默认值 'Unknown'
return bundle_id, app_name
except Exception as e:
print(f"Error reading plist: {e}")
return None, None
# 计算图片距离
def calculate_slide_distance(background_image, slider_image):
# 将 PIL Image 对象转换为 OpenCV 格式的图像
background = np.array(background_image)
slider = np.array(slider_image)
# OpenCV 使用 BGR 格式,需要转换为灰度图
background_gray = cv2.cvtColor(background, cv2.COLOR_RGB2GRAY) # PIL -> OpenCV
slider_gray = cv2.cvtColor(slider, cv2.COLOR_RGB2GRAY) # PIL -> OpenCV
# 使用Canny边缘检测来突出显示图像中的边缘
background_edges = cv2.Canny(background_gray, 100, 200) # 你可以调整阈值
slider_edges = cv2.Canny(slider_gray, 100, 200)
# 使用模板匹配找到滑块图像在背景图中的位置
result = cv2.matchTemplate(background_edges, slider_edges, cv2.TM_CCOEFF_NORMED)
# 获取最大匹配值的位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# max_loc 是模板匹配中最大相似度位置的左上角坐标
print(f"Max matching location (x, y): {max_loc}")
# 计算滑动的水平距离
slide_distance = background.shape[1] - (max_loc[0] + slider.shape[1])
print(f"Calculated slide distance: {slide_distance}")
# 可视化匹配位置,绘制矩形框显示匹配结果
top_left = max_loc
bottom_right = (top_left[0] + slider.shape[1], top_left[1] + slider.shape[0])
cv2.rectangle(background, top_left, bottom_right, (0, 255, 0), 2)
# 保存背景图、重叠图像和标记阴影图像为文件
cv2.imwrite("slider_matched.png", background)
# return slide_distance
return max_loc[0]
def checkSlider(index = 0, count = 10):
if index == count:
print("滑动次数用完")
return
if not exists(Template(r"tpl1739253902396.png", record_pos=(-0.299, 0.268), resolution=(1178, 2556))):
return
sleep(1.0)
# 或者指定保存路径
screen = G.DEVICE.snapshot("screenshot.png")
width, height = G.DEVICE.get_current_resolution()
slider_fullWidth = 900
slider_fullHeight = 680
startX = (width - slider_fullWidth)/2
startY = (height - slider_fullHeight)/2 - 105
item_width = 200
bg_width = slider_fullWidth - item_width
print(screen.size)
# 局部截图
slider_item = aircv.crop_image(screen, (startX, startY, startX+item_width, startY+slider_fullHeight))
aircv.imwrite("slider_item.png", slider_item)
slider_bg = aircv.crop_image(screen,(startX + item_width, startY, startX+slider_fullWidth, startY+slider_fullHeight))
# 保存局部截图到log文件夹中
aircv.imwrite("slider_bg.png", slider_bg)
s = time.time() # 局部找图开始计时
pos = calculate_slide_distance(slider_bg, slider_item)
print(pos)
print(f"设备宽度: {width}, 设备高度: {height}")
# # # 4. 获取匹配到的中心点坐标
vector_x = (pos + item_width) / width
print(f"pos: {pos}, item_width: {item_width} width: {width} vector_x:{vector_x}")
swipe(Template(r"tpl1739253902396.png", record_pos=(-0.299, 0.268), resolution=(1178, 2556)), vector=[vector_x, 0], duration=8)
sleep(5)
if exists(Template(r"tpl1739254855001.png", record_pos=(0.003, -0.426), resolution=(1178, 2556))):
checkSlider(index + 1, count)
else:
print("not -- not -- not")
def checkStart():
if exists(Template(r"tpl1739250741763.png", record_pos=(0.137, -0.282), resolution=(1178, 2556))):
touch(Template(r"tpl1739250765604.png", record_pos=(0.124, -0.272), resolution=(1178, 2556)))
def welcome():
if not exists(Template(r"tpl1739183827447.png", record_pos=(0.005, 0.818), resolution=(1178, 2556))):
return
if exists(Template(r"tpl1739183669926.png", record_pos=(0.195, 0.149), resolution=(1178, 2556))):
touch(Template(r"tpl1739183669926.png", record_pos=(0.195, 0.149), resolution=(1178, 2556)))
if exists(Template(r"tpl1739183718315.png", record_pos=(-0.014, 0.065), resolution=(1178, 2556))):
swipe(Template(r"tpl1739183718315.png", record_pos=(-0.014, 0.065), resolution=(1178, 2556)), vector=[1.0, 0])
if exists(Template(r"tpl1739183761807.png", record_pos=(-0.003, 0.119), resolution=(1178, 2556))):
swipe(Template(r"tpl1739183776147.png", record_pos=(0.014, 0.121), resolution=(1178, 2556)), vector=[1, 0])
if exists(Template(r"tpl1739183800467.png", record_pos=(-0.001, 0.073), resolution=(1178, 2556))):
swipe(Template(r"tpl1739183811394.png", record_pos=(0.012, 0.1), resolution=(1178, 2556)), vector=[1.0, 0])
if exists(Template(r"tpl1739183827447.png", record_pos=(0.005, 0.818), resolution=(1178, 2556))):
touch(Template(r"tpl1739183827447.png", record_pos=(0.005, 0.818), resolution=(1178, 2556)))
def testLoginPage():
if exists(Template(r"tpl1739183839459.png", record_pos=(0.194, -0.34), resolution=(1178, 2556))):
touch(Template(r"tpl1739183839459.png", record_pos=(0.194, -0.34), resolution=(1178, 2556)))
if exists(Template(r"tpl1739184179801.png", record_pos=(-0.122, -0.171), resolution=(1178, 2556))):
touch(Template(r"tpl1739184179801.png", record_pos=(-0.122, -0.171), resolution=(1178, 2556)))
text("a1@test.com")
if exists(Template(r"tpl1739183881875.png", record_pos=(0.303, 0.0), resolution=(1178, 2556))):
touch(Template(r"tpl1739183881875.png", record_pos=(0.303, 0.0), resolution=(1178, 2556)))
elif exists(Template(r"tpl1739254394652.png", record_pos=(0.331, 0.0), resolution=(1178, 2556))):
touch(Template(r"tpl1739254394652.png", record_pos=(0.331, 0.0), resolution=(1178, 2556)))
sleep(2)
checkSlider()
if exists(Template(r"tpl1739254286061.png", record_pos=(-0.234, 0.003), resolution=(1178, 2556))):
touch(Template(r"tpl1739254286061.png", record_pos=(-0.234, 0.003), resolution=(1178, 2556)))
text("1234")
touch(Template(r"tpl1739254312183.png", record_pos=(0.009, 0.194), resolution=(1178, 2556)))
app_path = "/xxx/xxx/xxx.app"
bundleId, appName = get_app_info(app_path)
checkStart()
welcome()
testLoginPage()
This post is licensed under CC BY 4.0 by the author.