提交 59df381e authored 作者: 陈泽健's avatar 陈泽健

调整正则表达式获取测试报告的百分比参数

上级 f0bfd85c
...@@ -116,41 +116,59 @@ import logging ...@@ -116,41 +116,59 @@ import logging
from PIL import Image from PIL import Image
import numpy as np import numpy as np
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') import logging
from PIL import Image
import numpy as np
import os
def compare_images_feature_matching(image1_path, image2_path): def compare_images_feature_matching(image1_path, image2_path):
try: try:
# 验证图片路径是否存在且为有效图片文件
if not os.path.isfile(image1_path) or not os.path.isfile(image2_path):
logging.error("图片路径无效")
return {"result": False, "error": "图片路径无效"}
# 打开两张图片 # 打开两张图片
img1 = Image.open(image1_path) img1 = Image.open(image1_path)
img2 = Image.open(image2_path) img2 = Image.open(image2_path)
# 将图片转换为相同的模式和尺寸 # 验证图片是否为有效格式
img1 = img1.convert("RGB") if img1.format not in ['JPEG', 'PNG'] or img2.format not in ['JPEG', 'PNG']:
img2 = img2.convert("RGB") logging.error("图片格式无效")
return {"result": False, "error": "图片格式无效"}
# 如果尺寸不同,先调整大小 # 如果尺寸不同,先调整大小
if img1.size != img2.size: if img1.size != img2.size:
logging.info("图片尺寸不同,调整为相同尺寸进行比较") logging.info("图片尺寸不同,调整为相同尺寸进行比较")
img2 = img2.resize(img1.size, Image.ANTIALIAS) # 保持纵横比 img2 = img2.resize(img1.size, Image.ANTIALIAS) # 保持纵横比
# 将图片转换为相同的模式
img1 = img1.convert("RGB")
img2 = img2.convert("RGB")
# 转换为 numpy 数组进行比较 # 转换为 numpy 数组进行比较
img1_array = np.array(img1) img1_array = np.array(img1)
img2_array = np.array(img2) img2_array = np.array(img2)
# 输出numpy数组信息
logging.info(f"图片1数组: {img1_array},图片2数组: {img2_array}")
# 比较两个数组是否相同 # 比较两个数组是否相同
return np.array_equal(img1_array, img2_array) return {"result": np.array_equal(img1_array, img2_array), "error": None}
except FileNotFoundError as e: except FileNotFoundError as e:
logging.error(f"文件未找到: {e}") logging.error(f"文件未找到: {e}")
return False return {"result": False, "error": f"文件未找到: {e}"}
except OSError as e: except OSError as e:
logging.error(f"图片处理错误: {e}") logging.error(f"图片处理错误: {e}")
return False return {"result": False, "error": f"图片处理错误: {e}"}
except MemoryError as e: except MemoryError as e:
logging.error(f"内存不足: {e}") logging.error(f"内存不足: {e}")
return False return {"result": False, "error": f"内存不足: {e}"}
except Exception as e:
logging.error(f"未知错误: {e}")
return {"result": False, "error": f"未知错误: {e}"}
# if __name__ == '__main__': # if __name__ == '__main__':
......
...@@ -1016,6 +1016,10 @@ def play_cloud_voice(app_id, app_secret, device_sn): ...@@ -1016,6 +1016,10 @@ def play_cloud_voice(app_id, app_secret, device_sn):
# device_sn = os.getenv("DEVICE_SN", "W703BB44444") # device_sn = os.getenv("DEVICE_SN", "W703BB44444")
# play_cloud_voice(app_id, app_secret, device_sn) # play_cloud_voice(app_id, app_secret, device_sn)
import logging
import re
from selenium.webdriver.common.by import By
def get_test_result(latest_report, wd): def get_test_result(latest_report, wd):
""" """
获取测试结果页面的通过率、失败率和异常率 获取测试结果页面的通过率、失败率和异常率
...@@ -1033,39 +1037,46 @@ def get_test_result(latest_report, wd): ...@@ -1033,39 +1037,46 @@ def get_test_result(latest_report, wd):
# 访问测试结果页面 # 访问测试结果页面
wd.get(latest_report) wd.get(latest_report)
sleep(5)
# 获取通过率 # 点击简略显示
pass_percent = elment_get_text((By.CSS_SELECTOR, "div[class='result_barchart'] div:nth-child(1) span:nth-child(1)"), safe_click((By.XPATH,"//div[@id='display_mode']"), wd)
wd) sleep(5)
logging.info(f"获取的通过率:{pass_percent}")
match = re.search(r'(\d+)%', pass_percent) # 定义一个函数来获取和解析百分比
def get_percentage(selector, wd):
text = elment_get_text(selector, wd)
logging.info(f"获取的文本:{text}")
match = re.search(r'(\d+(\.\d+)?)%', text)
if match: if match:
pass_percent = match.group(0) return match.group(0)
else: else:
pass_percent = "0" logging.error(f"未找到百分比匹配项,文本内容: {text}")
return "0"
# 获取通过率
pass_percent = get_percentage((By.CSS_SELECTOR, "div[class='result_barchart'] div:nth-child(1) span:nth-child(1)"), wd)
test_result["pass_percent"] = pass_percent test_result["pass_percent"] = pass_percent
# 获取失败率 # 获取失败率
fail_percent = elment_get_text( fail_percent = get_percentage(
(By.CSS_SELECTOR, "body > div.main_section > div.result > div > div:nth-child(2) > span"), wd) (By.CSS_SELECTOR, "body > div.main_section > div.result > div > div:nth-child(2) > span"), wd)
logging.info(f"获取的失败率:{fail_percent}")
match = re.search(r'(\d+)%', fail_percent)
if match:
fail_percent = match.group(0)
else:
fail_percent = "0"
test_result["fail_percent"] = fail_percent test_result["fail_percent"] = fail_percent
# 获取异常率 # 获取异常率
exception_percent = elment_get_text( exception_percent = get_percentage(
(By.CSS_SELECTOR, "body > div.main_section > div.result > div > div:nth-child(3) > span"), wd) (By.CSS_SELECTOR, "body > div.main_section > div.result > div > div:nth-child(3) > span"), wd)
logging.info(f"获取的异常率:{exception_percent}")
match = re.search(r'(\d+)%', exception_percent)
if match:
exception_percent = match.group(0)
else:
exception_percent = "0"
test_result["exception_percent"] = exception_percent test_result["exception_percent"] = exception_percent
# 输出test_result
logging.info(test_result)
print(test_result)
sleep(5)
# 返回测试结果字典 # 返回测试结果字典
return test_result return test_result
# if __name__ == "__main__":
# browser_init("展厅预定巡检")
# wd = GSTORE['wd']
# test_result = get_test_result("http://nat.ubainsyun.com:31134/report_20250217_094401.html",wd)
# print(test_result)
\ No newline at end of file
...@@ -213,4 +213,4 @@ ...@@ -213,4 +213,4 @@
- 补充讯飞语音转录功能流程,调试通过。 - 补充讯飞语音转录功能流程,调试通过。
- 补充实现无纸化同屏巡检流程,但是还存在图片匹配失败问题。处理优化。 - 补充实现无纸化同屏巡检流程,但是还存在图片匹配失败问题。处理优化。
55. 2025-02-17 55. 2025-02-17
- 处理无纸化同屏操作流程。 - 处理无纸化同屏操作流程。调整正则表达式获取测试报告的百分比参数。
\ No newline at end of file \ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论