基于Robot Framework实现Web表格多行数据自动化测试
Robot Framework 动态表格自动化测试实现方案
针对行数不固定的Web表格,以下是两个动态测试场景的实现方案,解决原脚本仅支持单行测试的问题:
场景1:逐行遍历表格并处理符合条件的行
需求说明
遍历表格所有行,获取每行的ProductID、Status、Remarks信息;当Status为Good或Excellent时,点击当前行的Process按钮。
改造后代码
# 获取表格总行数 ${total_rows} = Get Element Count xpath=//*[@id="MonthlyReport"]/tbody/tr Log To Console 表格总行数:${total_rows} # 遍历每一行(从第1行到最后一行) FOR ${row_num} IN RANGE 1 ${total_rows + 1} # 获取当前行的ProductID ${product_id} Get Text xpath=//*[@id="MonthlyReport"]/tbody/tr[${row_num}]/td[1] Log To Console 当前行ProductID:${product_id} # 获取当前行的Status ${status} = Get Text xpath=//*[@id="MonthlyReport"]/tbody/tr[${row_num}]/td[9] Log To Console ${product_id} 的状态:${status} # 获取当前行的Remarks(修正原代码表格ID不一致问题,统一为MonthlyReport) ${remarks} = Get Text xpath=//*[@id="MonthlyReport"]/tbody/tr[${row_num}]/td[10] Log To Console ${product_id} 的备注:${remarks} # 判断Status是否为Good或Excellent,是则点击当前行的Process按钮 IF '${status}' == 'Good' OR '${status}' == 'Excellent' # 定位当前行的Process按钮(需根据实际页面调整xpath,确保关联到当前行) Click Element xpath=//*[@id="MonthlyReport"]/tbody/tr[${row_num}]//button[@id="Process"] Log To Console 已点击${product_id}对应的Process按钮 END END
关键优化点
- 使用
FOR循环遍历所有行,替代原脚本仅处理最后一行的逻辑 - 将
Process按钮的定位与当前行绑定,避免点击错误按钮 - 修正原代码中表格ID不一致的问题(原脚本混用
MonthlyReport和tableChargebacks)
场景2:遍历所有ProductID并验证搜索功能
需求说明
遍历表格中所有ProductID,逐个输入搜索框,验证搜索结果中存在对应ID的行。
改造后代码
# 步骤1:收集表格中所有ProductID到列表 ${total_rows} = Get Element Count xpath=//*[@id="MonthlyReport"]/tbody/tr @{product_ids} = Create List FOR ${row_num} IN RANGE 1 ${total_rows + 1} ${id} Get Text xpath=//*[@id="MonthlyReport"]/tbody/tr[${row_num}]/td[1] Append To List ${product_ids} ${id} END Log To Console 收集到的ProductID列表:${product_ids} # 步骤2:遍历每个ProductID执行搜索验证 FOR ${target_id} IN @{product_ids} # 清空搜索框并输入目标ID Input Text //input[@placeholder='Search ...'] ${target_id} Sleep 2 # 等待搜索结果加载,可根据实际情况调整时间 # 验证搜索结果中存在该ProductID的行 ${result_exists} = Run Keyword And Return Status Element Should Exist xpath=//*[@id="MonthlyReportSearch"]/tbody/tr/td[1][text()="${target_id}"] IF ${result_exists} Log To Console 搜索${target_id}成功,结果存在 ELSE Fail 搜索${target_id}失败,未找到对应行 END # 清空搜索框,准备下一次搜索 Input Text //input[@placeholder='Search ...'] END
关键优化点
- 先收集所有
ProductID到列表,再批量遍历验证 - 使用文本匹配的xpath结合
Element Should Exist,精准验证目标行是否存在 - 添加清空搜索框步骤,避免残留内容影响下一次搜索
内容的提问来源于stack exchange,提问作者ADS KUL
相关产品推荐
相关产品推荐

