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

Karate测试框架中动态Excel文件上传问题求助

解决Karate测试中更新Excel后API读取旧文件的问题

核心问题分析

  • 你更新的是src/test/java/listingsmanagement/utilities/marketable-location-to-create.xlsx,但上传的是target/test-classes/...路径下的文件。构建工具(如Maven)会将src目录的资源复制到target,更新src文件后target里的旧文件未同步,导致API读取的是未更新版本。
  • Apache POI操作Excel时如果未正确关闭文件流,可能导致修改内容未真正写入磁盘。
  • Thread.sleep()不是可靠的文件写入等待方式,无法保证文件操作完成。

具体解决方案

1. 统一文件路径

直接使用src目录下的文件路径上传,避免构建工具复制延迟问题:

* def filepath = 'file:/Users/palanisankar/listings-management-projects/listings-management/automation/src/test/java/listingsmanagement/utilities/marketable-location-to-create.xlsx'

2. 确保POI正确关闭文件流

检查MLocationCreateFileWritter.writeMarketableLocationToExcel()方法,用try-with-resources语法自动关闭流,确保内容写入磁盘:

try (FileOutputStream fos = new FileOutputStream(filePath);
     XSSFWorkbook workbook = new XSSFWorkbook()) {
    // 执行Excel写入操作
    workbook.write(fos);
} catch (IOException e) {
    // 异常处理逻辑
}

3. 移除无效的线程睡眠

删除java.lang.Thread.sleep(10000),依赖POI的流关闭机制确保文件写入完成,无需额外等待。

4. 对齐读取与上传路径

确认MLocationCreateSheetParser.convertExcelToJson()读取的是和上传一致的文件路径,避免路径不一致导致的内容差异。

调整后的代码示例

Background: Pre-Requisites
  * def reachServicesUrl = karate.get('reachApiBaseUrl')
  * def envJwtToken = Java.type('listingsmanagement.fixtures.EnvConfig')
  * def jwtToken = envJwtToken.get('JWT_TOKEN')
  * def excelFilePath = "src/test/java/listingsmanagement/utilities/marketable-location-to-create.xlsx"
  * def triggerMarketableLocationIdToExcelWritterClass = Java.type("listingsmanagement.fixtures.MLocationCreateFileWritter")
  * def writeMarketableLocationIdToExcel = triggerMarketableLocationIdToExcelWritterClass.writeMarketableLocationToExcel(excelFilePath, "LC001")
  * def parsingMarketableLocationIds = Java.type("listingsmanagement.fixtures.MLocationCreateSheetParser")
  * def getMarketableLocationIds = parsingMarketableLocationIds.convertExcelToJson(excelFilePath)
  * def filepath = 'file:/Users/palanisankar/listings-management-projects/listings-management/automation/' + excelFilePath

@Test11
Scenario: Listing successfully created for an available marketable location
  Given url reachServicesUrl
  And path 'marketable-location', 'create'
  And header Content-Type = 'multipart/form-data'
  And cookie dev-authorization = jwtToken
  * karate.log('Marketable Location Ids: ', getMarketableLocationIds)
  * def fileUpload =
    """
    {
      "read": "#(filepath)",
      "filename": "marketable-location-to-create",
      "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    }
    """
  And multipart file locationCreateFile = fileUpload
  When method Post
  Then status 200
  * def marketableLocationIdsArray = $getMarketableLocationIds[*].['Marketable Location Id']
  * print 'Marketable Location Ids Array: ', marketableLocationIdsArray

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 20:22:33