测试目的:判断当前步骤截取的图片上方是否大部分为绿色
代码:
# -*- encoding=utf8 -*-
__author__ = "Sodnrn"
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
from os import getcwd
from PIL import Image
# 获取当前工作目录
pic_path = getcwd()
# 截图并将图片保存到当前工作目录
snapshot(filename=f"{pic_path}/PicColor.png", msg="获取图片上方颜色")
# 读取图片并获取图片尺寸
img = Image.open(f"{pic_path}/PicColor.png")
width, height = img.size
# 处理图片
top_color = []
# 获取图片上方的RGB值,并将绿色范围内的收集进入列表
for colu in range(0, width):
bar_color = img.getpixel((colu, 0.03*height))
print(bar_color)
if bar_color[0] < 20 and 245<= bar_color[1] <= 255 and bar_color[2] < 20:
top_color.append(bar_color)
# 计算绿色的占比
green_rate = len(top_color)/width
print(green_rate)
if green_rate > 0.5:
print('pass')
输出:
(2, 250, 3)
(4, 250, 3)
(0, 248, 0)
(2, 255, 6)
(5, 255, 9)
(7, 251, 5)
(21, 248, 8)
(16, 249, 7)
(0, 255, 2)
(0, 255, 11)
0.9972222222222222
pass