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
import sys
import os
import openpyxl
import json
from 预定系统.Base.base import INFO
# 获取当前脚本的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建预定系统的绝对路径
预定系统_path = os.path.abspath(os.path.join(current_dir, '..', '..', '..', '..'))
# 添加路径
sys.path.append(预定系统_path)
# 导入模块
from 预定系统.Base.base import *
# 获取当前脚本所在的目录
current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../../测试数据/登录模块/新账号密码登录.xlsx')
def read_xlsx_data(xlsx_file_path):
"""
读取XLSX文件中的数据,并将其转换为一个包含字典的列表,每个字典代表一行测试用例数据。
参数:
xlsx_file_path (str): XLSX文件的路径。
返回:
list: 包含字典的列表,每个字典包含测试用例的名称和参数。
"""
# 打开XLSX文件
workbook = openpyxl.load_workbook(xlsx_file_path)
# 假设数据在第一个工作表中
sheet = workbook.active
# 读取表头,从第三行开始
headers = [cell.value for cell in sheet[3]]
# 打印表头列名
# print(f"表头列名: {headers}")
# 找到表头中名为 'JSON' 的列索引
try:
json_index = headers.index('JSON')
except ValueError as e:
raise ValueError(f"表头中没有找到所需的列: {e}")
ddt_cases = []
# 遍历XLSX文件中的每一行数据,从第四行开始
for row in sheet.iter_rows(min_row=4, values_only=True):
# 获取 JSON 列的数据
json_data = row[json_index]
# 打印 JSON 数据以进行调试
# print(f"JSON 数据: {json_data}")
# 解析 JSON 字符串
try:
if json_data:
parsed_json = json.loads(json_data)
else:
raise ValueError("JSON 数据为空")
except json.JSONDecodeError:
raise ValueError(f"无法解析 JSON 数据: {json_data}")
# 将解析后的 JSON 数据添加到列表中
ddt_cases.append(parsed_json)
# 日志记录:XLSX文件已读取
INFO("XLSX文件已读取")
# 返回包含所有测试用例数据的列表
return ddt_cases
if __name__ == '__main__':
ddt_cases = read_xlsx_data(csv_file_path)
# print(ddt_cases)
browser_init("标准版预定系统")
wd = GSTORE['wd']
# 遍历 ddt_cases 并获取每一个 JSON 对象
for case in ddt_cases:
for step in case:
print(f"当前步骤: {step}")
# 你可以在这里对 step 进行进一步处理
# 例如:访问特定字段
# page = step.get('page')
# 调用get_by_enum将定位类型转为枚举
locator_type = get_by_enum(step.get('locator_type'))
locator_value = step.get('locator_value')
element_type = step.get('element_type')
element_value = step.get('element_value')
#
# print(f"页面: {page}")
# print(f"元素定位类型: {locator_type}")
# print(f"元素定位值: {locator_value}")
# print(f"元素类型: {element_type}")
# print(f"输入值: {element_value}")
safe_send_keys((locator_type, locator_value), element_value, wd)
sleep(5)