You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Karate中读取URL下载的xlsx文件内容并完成校验

Karate直接校验xlsx接口响应内容的方案

完全可以不用将响应写入本地文件,直接在内存中解析校验xlsx内容即可。你尝试将响应转字符串得到乱码的原因很简单:xlsx是二进制压缩格式,并非普通文本格式,直接将二进制字节流强转为字符串自然会出现乱码。

你不需要对responseBytes做转字符串的操作,直接用原始的二进制字节流就可以完成解析校验,具体实现步骤如下:

  • 第一步:项目引入Apache POI依赖,这是Java生态常用的Excel解析工具,Karate支持直接调用Java代码,所以可以直接用POI在内存中解析二进制流。
  • 第二步:封装Excel解析工具类,接收字节数组入参,返回方便Karate匹配的结构化数据,示例代码如下:
import org.apache.poi.ss.usermodel.*;
import java.io.ByteArrayInputStream;
import java.util.*;

public class ExcelUtils {
    public static List<Map<String, Object>> parseXlsx(byte[] xlsxBytes) throws Exception {
        List<Map<String, Object>> sheetData = new ArrayList<>();
        // 直接从二进制流创建Workbook,无需写入本地文件
        try (Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(xlsxBytes))) {
            // 按需读取对应sheet,示例取第一个sheet
            Sheet sheet = workbook.getSheetAt(0);
            // 读取表头行
            Row headerRow = sheet.getRow(0);
            List<String> headers = new ArrayList<>();
            for (Cell cell : headerRow) {
                headers.add(cell.getStringCellValue().trim());
            }
            // 遍历读取数据行
            for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row dataRow = sheet.getRow(rowIndex);
                Map<String, Object> rowMap = new HashMap<>();
                for (int colIndex = 0; colIndex < headers.size(); colIndex++) {
                    Cell cell = dataRow.getCell(colIndex);
                    Object cellValue = null;
                    if (cell != null) {
                        switch (cell.getCellType()) {
                            case STRING:
                                cellValue = cell.getStringCellValue().trim();
                                break;
                            case NUMERIC:
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    cellValue = cell.getDateCellValue();
                                } else {
                                    cellValue = cell.getNumericCellValue();
                                }
                                break;
                            case BOOLEAN:
                                cellValue = cell.getBooleanCellValue();
                                break;
                            default:
                                cellValue = null;
                        }
                    }
                    rowMap.put(headers.get(colIndex), cellValue);
                }
                sheetData.add(rowMap);
            }
        }
        return sheetData;
    }
}
  • 第三步:在Karate脚本中调用工具类解析响应,直接匹配校验内容,你的原有接口请求逻辑不需要修改,只需要新增解析和匹配部分即可:
Given url url1
When method POST
Then status 200
And match header Content-disposition contains 'attachment'
And match header Content-disposition contains 'example.xlsx'
And match header Content-Type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
# 调用Java工具类解析xlsx二进制内容
* def ExcelUtils = Java.type('替换为你自己的工具类包名.ExcelUtils')
* def excelContent = ExcelUtils.parseXlsx(responseBytes)
# 按需校验内容,示例校验第一行的姓名列值为张三
* match excelContent[0].姓名 == '张三'
# 也可以批量匹配整行结构
* match excelContent[1] == {年龄: 25, 部门: '技术部', 在职状态: true}

内容的提问来源于stack exchange,提问作者darklord84

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 23:39:04