Karate Xplorer动态传参、存储SOAP响应及定制报告咨询
Karate Xplorer SOAP API测试需求解决方案
1. 动态传入批量MRN值
Karate Xplorer完全支持批量参数化,无需手动逐个输入MRN,两种常用实现方式:
- 直接在脚本中定义/加载批量MRN:
Scenario: 批量执行SOAP请求 * def mrns = read('mrns.json') // 从外部JSON文件加载,格式为["MRN001", "MRN002"] * def soapTemplate = read('soap-request-template.xml') // 模板中用#(mrn)作为MRN占位符 * call each mrns * set soapTemplate // 将当前循环的MRN替换到模板对应位置 * soap soapTemplate // 发送SOAP请求
- 用Scenario Outline实现参数化:
Scenario Outline: 批量测试SOAP API * def soapRequest = read('soap-request-template.xml') * set soapRequest // 替换MRN占位符 * soap soapRequest Examples: | mrn | | MRN001 | | MRN002 | | MRN003 |
如果MRN数据量较大,推荐用read('mrns.csv')从CSV文件加载,无需修改测试脚本。
2. 存储每个MRN的API响应到Excel/其他格式
Karate Xplorer继承了Karate的文件操作能力,可灵活存储响应:
- 按MRN单独保存JSON文件:
* call each mrns * set soapTemplate * soap soapTemplate * write response to 'response-MRN#(mrn).json' // 每个MRN对应一个独立响应文件
- 批量写入CSV(可直接转Excel):
* def resultList = [] * call each mrns * set soapTemplate * soap soapTemplate * resultList.push({ mrn: '#(mrn)', response: response }) * write resultList to 'mrn-responses.csv'
- 直接生成Excel文件:通过内置JavaScript调用Apache POI库,无需额外依赖:
* def createExcel = function(results) { var workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(); var sheet = workbook.createSheet('MRN测试响应'); // 写入表头 var headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue('MRN'); headerRow.createCell(1).setCellValue('API响应'); // 写入每行数据 for (var i = 0; i < results.length; i++) { var row = sheet.createRow(i + 1); row.createCell(0).setCellValue(results[i].mrn); row.createCell(1).setCellValue(JSON.stringify(results[i].response)); } var fos = new java.io.FileOutputStream('mrn-responses.xlsx'); workbook.write(fos); fos.close(); return 'Excel文件已生成'; } * call createExcel(resultList)
3. 报告定制支持
Karate Xplorer完全支持报告定制,三种常用方式:
- 修改报告基础配置:在
karate-config.js中添加报告参数,自定义标题、logo等:
karate.configure('report', { title: 'SOAP API MRN批量测试报告', logo: 'src/test/resources/custom-logo.png', showLog: false // 隐藏请求日志,简化报告内容 });
- 自定义HTML报告模板:将Karate默认的HTML报告模板复制到项目
src/test/resources/karate-reports目录,修改HTML结构、CSS样式,Karate Xplorer会自动加载自定义模板生成报告。 - 导出结果自行生成报告:测试完成后,Karate Xplorer会生成
karate-results.json文件,包含所有测试用例的执行结果和响应数据,你可以基于这个文件编写脚本生成Excel、PDF等定制化报告。
内容的提问来源于stack exchange,提问作者Lavanya Yarasu
相关产品推荐
相关产品推荐

