提交 710b0804 authored 作者: 陈泽健's avatar 陈泽健

refactor(预定系统): 优化 XLSX 文件处理逻辑

- 简化工作表选择逻辑
- 增加 JSON 数据解析的错误处理和日志记录
- 优化 XLSX 文件处理完成后的日志输出
- 修复兰州中石化项目测试用例的路径和登录逻辑
上级 7139b877
...@@ -485,18 +485,11 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None): ...@@ -485,18 +485,11 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None):
workbook = openpyxl.load_workbook(xlsx_file_path, read_only=True) workbook = openpyxl.load_workbook(xlsx_file_path, read_only=True)
INFO("XLSX文件成功打开") INFO("XLSX文件成功打开")
# === 修改后的工作表选择逻辑 === sheets_to_process = [workbook[sheet_name]] if sheet_name else workbook.worksheets
if sheet_name:
sheets_to_process = [workbook[sheet_name]] # 处理指定工作表
else:
sheets_to_process = workbook.worksheets # 处理所有工作表
# ==========================
ddt_cases = [] ddt_cases = []
for sheet in sheets_to_process: for sheet in sheets_to_process:
INFO(f"正在处理工作表: {sheet.title}") INFO(f"正在处理工作表: {sheet.title}")
headers = [cell.value for cell in sheet[3]] headers = [cell.value for cell in sheet[3]]
INFO(f"表头列名: {headers}") INFO(f"表头列名: {headers}")
...@@ -505,14 +498,18 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None): ...@@ -505,14 +498,18 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None):
category_index = headers.index('功能类别') category_index = headers.index('功能类别')
except ValueError as e: except ValueError as e:
INFO(f"工作表 {sheet.title} 缺少必要列,跳过: {e}") INFO(f"工作表 {sheet.title} 缺少必要列,跳过: {e}")
continue # 跳过不符合格式的工作表 continue
for row_num, row in enumerate(sheet.iter_rows(min_row=4, values_only=True), start=4): for row_num, row in enumerate(sheet.iter_rows(min_row=4, values_only=True), start=4):
json_data = row[json_index] json_data = row[json_index]
if not json_data or not json_data.strip(): if not json_data or not json_data.strip():
INFO(f"行 {row_num} 的JSON数据为空,跳过")
continue continue
try: try:
# 打印原始JSON数据用于调试
INFO(f"行 {row_num} 原始JSON数据: {json_data}")
parsed_json = json.loads(json_data) parsed_json = json.loads(json_data)
category = row[category_index] category = row[category_index]
...@@ -520,14 +517,18 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None): ...@@ -520,14 +517,18 @@ def read_xlsx_data(xlsx_file_path, sheet_name=None, case_type=None):
continue continue
ddt_cases.append(parsed_json) ddt_cases.append(parsed_json)
except json.JSONDecodeError: INFO(f"行 {row_num} JSON解析成功: {parsed_json}")
INFO(f"行 {row_num} 的 JSON 数据解析失败") except json.JSONDecodeError as e:
logging.error(f"行 {row_num} 的JSON数据解析失败: {e}\n数据内容: {json_data}")
except Exception as e:
logging.error(f"行 {row_num} 处理时发生未知错误: {e}")
INFO("XLSX文件处理完成") INFO(f"XLSX文件处理完成,共找到 {len(ddt_cases)} 条用例")
return ddt_cases return ddt_cases
except Exception as e: except Exception as e:
raise Exception(f"处理文件时出错: {e}") logging.error(f"处理文件时出错: {e}")
raise
import openpyxl import openpyxl
......
...@@ -325,4 +325,5 @@ ...@@ -325,4 +325,5 @@
- 标准版注释clear_columns_in_xlsx和write_xlsx_data方法调用。定时任务补充兰州。 - 标准版注释clear_columns_in_xlsx和write_xlsx_data方法调用。定时任务补充兰州。
96. 2025-06-18: 96. 2025-06-18:
- base增加read_xlsx_data_auto函数,支持sheet_name为None时,默认遍历整个测试用例所有模块从第一个sheet到最后的sheet执行,若sheet_name传参时,则为单独执行某个模块测试。 - base增加read_xlsx_data_auto函数,支持sheet_name为None时,默认遍历整个测试用例所有模块从第一个sheet到最后的sheet执行,若sheet_name传参时,则为单独执行某个模块测试。
- 删除原函数,避免混淆。 - 删除原函数,避免混淆。
\ No newline at end of file - read_xlsx_data函数优化,调试兰州中石化项目自动化测试。
\ No newline at end of file
...@@ -11,7 +11,7 @@ sys.path.append(预定系统_path) ...@@ -11,7 +11,7 @@ sys.path.append(预定系统_path)
from 预定系统.Base.base import * from 预定系统.Base.base import *
# 构建XLSX文件的绝对路径 (修正路径) # 构建XLSX文件的绝对路径 (修正路径)
xlsx_file_path = os.path.join(预定系统_path, '测试数据', '兰州中石化项目测试用例全局测试.xlsx') xlsx_file_path = os.path.join(预定系统_path, '测试数据', '兰州中石化项目测试用例.xlsx')
class LanzhouSinopecProject: class LanzhouSinopecProject:
tags = ['合并测试'] tags = ['合并测试']
...@@ -36,6 +36,12 @@ class LanzhouSinopecProject: ...@@ -36,6 +36,12 @@ class LanzhouSinopecProject:
wd.refresh() wd.refresh()
wd.refresh() wd.refresh()
if "议题申报" in name:
# 点击【议题申报】按钮进入模块
INFO("点击【议题申报】按钮")
safe_click((By.XPATH, "//div[@id='CreateTopic']"), wd)
sleep(1)
for step in self.para: for step in self.para:
# 赋值页面类型page # 赋值页面类型page
page_type = step.get('page') page_type = step.get('page')
...@@ -63,9 +69,14 @@ class LanzhouSinopecProject: ...@@ -63,9 +69,14 @@ class LanzhouSinopecProject:
sleep(2) sleep(2)
SELENIUM_LOG_SCREEN(wd, "75") SELENIUM_LOG_SCREEN(wd, "75")
elif element_type == "SwitchWindow":
wd.switch_to.window(wd.window_handles[element_value])
sleep(2)
SELENIUM_LOG_SCREEN(wd, "75")
elif element_type == "login": elif element_type == "login":
safe_click((By.XPATH, "//div[contains(@class,'quit')]"), wd)
sleep(2) sleep(2)
INFO(f"开始登录,账号为:{element_value[0]},密码为:{element_value[1]}")
user_login(element_value[0],element_value[1]) user_login(element_value[0],element_value[1])
sleep(2) sleep(2)
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论