python+uiautomator2判断app是否进入到闪屏广告页面
- 前提背景:app内部存在多处广告,需要进行进行自动化:
1. 查看app是否成功跳转了页面
2. 页面是否空白
3. 大致经历的耗时 - 主要思路如下:点击前进行截图操作,点击后进行判断
- 判断图片是否空白
12345678910111213141516171819202122232425262728293031def
is_blank(image_path, gray_value
=
250
, threshold
=
0.9
):
"""
函数会计算一幅图像中超过250(在0到255的灰度值中,255代表白色)的像素的比例,
如果这个比例超过了阈值(默认为0.05,即5%),那么就判断这幅图像为空白。
:param image_path:
:param gray_value:
:param threshold:
:return:
"""
# 记录函数开始运行的时间
# start_time = time.time()
# 打开图像
img
=
Image.
open
(image_path)
# 将图像转换为灰度模式
img
=
img.convert(
'L'
)
# 将图像转换为numpy数组
img_array
=
np.asarray(img)
# 计算图像的总像素
total_pixels
=
img_array.shape[
0
]
*
img_array.shape[
1
]
# 计算图像中白色(或者接近白色)像素的数量
white_pixels
=
np.
sum
(img_array > gray_value)
# 如果图像中超过一定比例的像素是白色(或接近白色),则判断图像为空白
# 记录函数结束运行的时间
# end_time = time.time()
# 计算并打印函数运行的总时间
# elapsed_time = end_time - start_time
# print(f"The function took {elapsed_time} seconds to complete.")
return
white_pixels
/
total_pixels > threshold
- 图片比较是否相同
12345678910111213141516171819def
is_same_image(file1, file2, threshold
=
10
):
# 打开图片
img1
=
Image.
open
(file1)
img2
=
Image.
open
(file2)
# 对比基本信息
if
img1.size !
=
img2.size
or
img1.mode !
=
img2.mode:
return
False
# 对比像素信息
for
i
in
range
(img1.size[
0
]):
for
j
in
range
(img1.size[
1
]):
p1
=
img1.getpixel((i, j))
p2
=
img2.getpixel((i, j))
diff
=
sum
([
abs
(p1[k]
-
p2[k])
for
k
in
range
(
3
)])
if
diff > threshold:
return
False
# 两张图片一致
return
True
- 最后删除图片
123456789def
delete(image_path):
"""
删除图片
:param image_path:
:return:
"""
if
os.path.isfile(image_path):
os.remove(image_path)
print
(
'图片删除成功'
)
- 获取当前app的activity
123456789101112def
get_current_activity():
"""
获取当前页面的activity
:return:
"""
cmd
=
'adb -s {} shell "dumpsys window | grep mCurrentFocus"'
.
format
(
get_android_devices())
activity_info
=
invoke(cmd).splitlines()
log.info(activity_info)
activity
=
str
(activity_info[
0
]).split(
"/"
)[
-
1
][:
-
1
]
log.info(activity)
return
activity
- 完整代码
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import
os<br>
from
PIL
import
Image<br>
import
numpy as np<br>activity1
=
get_current_activity()
save_path
=
os.path.join(os.getcwd(),
'screenshots'
)
# 保存截图的本地路径
if
not
os.path.exists(save_path):
os.makedirs(save_path)
screenshot_name
=
"闪屏点击前图片.png"
screenshot_path1
=
os.path.join(save_path, screenshot_name)
# 图片保存路径
d.screenshot(screenshot_path1)
if
d(textContains
=
'第三方应用'
).exists:
d(textContains
=
'第三方应用'
).click()
else
:
assert
False
,
"不存在对应按钮,需要重新载入新方法"
start_time
=
time.time()
time.sleep(
3
)
# 截屏
screenshot_name
=
"闪屏.png"
screenshot_path
=
os.path.join(save_path, screenshot_name)
# 图片保存路径
d.screenshot(screenshot_path)
for
i
in
range
(
0
,
3
):
if
is_blank(screenshot_path):
delete(screenshot_path)
time.sleep(
1
)
screenshot_name
=
"闪屏.png"
screenshot_path
=
os.path.join(save_path, screenshot_name)
# 图片保存路径
d.screenshot(screenshot_path)
end_time
=
time.time()
elapsed_time
=
end_time
-
start_time
if
i
=
=
2
:
assert
False
,
"经过{}秒,当前页面仍然空白"
.
format
(elapsed_time)
else
:
delete(screenshot_path)
end_time
=
time.time()
elapsed_time
=
end_time
-
start_time
print
(
"经过{}秒,进入页面"
.
format
(elapsed_time))
break
# 判断跳转到别的app
screenshot_name
=
"闪屏点击后图片.png"
screenshot_path2
=
os.path.join(save_path, screenshot_name)
# 图片保存路径
d.screenshot(screenshot_path2)
if
is_same_image(screenshot_path1, screenshot_path2):
assert
False
,
"没有跳转任何页面"
else
:
if
api.get_current_app() !
=
'xxx'
:
print
(
"跳转到别的app【{}】"
.
format
(api.get_current_app()))
else
:
# 判断跳转到别的非首页的activity
if
get_current_activity() !
=
activity1:
print
(
"跳转到当前app页面:【{}】"
.
format
(get_current_activity()))
else
:
print
(
"在app当前页面弹出框"
)
d.app_start(
'xxx包'
,
'xx activity'
)
即可进行闪屏点击,判断跳转进入的页面
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】