最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Handling File Uploads with Dynamic Excel File in Karate Test Framework - Stack Overflow

programmeradmin3浏览0评论

During test execution, the Excel file is updated using Apache POI. However, when I attempt to pass the updated file as multipart in the request, the API consistently picks up the old file instead of the modified version. Can anyone provide assistance with this? My code is as follows:

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

@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

I need to update an Excel sheet and then pass it in as multipart in the request.

During test execution, the Excel file is updated using Apache POI. However, when I attempt to pass the updated file as multipart in the request, the API consistently picks up the old file instead of the modified version. Can anyone provide assistance with this? My code is as follows:

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

@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

I need to update an Excel sheet and then pass it in as multipart in the request.

Share Improve this question edited Mar 16 at 7:22 tucuxi 18k2 gold badges47 silver badges80 bronze badges asked Mar 14 at 9:13 Kash SkKash Sk 311 silver badge2 bronze badges 7
  • If a file is being uploaded by this test, and the contents of this file are old, then it can hardly be the fault of the test - Karate cannot make up file contents, so you must be passing in the path to an old version. Am I understanding the problem correctly? – tucuxi Commented Mar 14 at 13:56
  • Hey tucuxi, Thanks for your response I understand that Karate cannot modify the file contents on its own.However, in my test, I update the Excel file using Apache POI before passing it as a multipart request. The issue is that the API still receives the old version of the file instead of the updated one. I have verified that the file is modified and saved before sending the request. Is there any possibility that Karate caches the file, or do I need to explicitly refresh or reload it before sending the request? Any suggestions on how to ensure the updated file is picked up. – Kash Sk Commented Mar 14 at 18:03
  • I would save it with a different name, and see if it still picks it up wrong. Your code is incomplete, and I would need a reproducible example to actually test it. – tucuxi Commented Mar 16 at 8:31
  • I have identified the solution. – Kash Sk Commented Mar 16 at 18:37
  • 1 Any updates to the Excel sheet should be applied to the copied version in the test-classes folder (or the target directory). This ensures that the latest version of the sheet is used during test execution or runtime. – Kash Sk Commented Mar 16 at 18:38
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Fixed Code:

`

Background: Pre-Requisites
  * def ServicesBaseUrl = karate.get('BaseUrl')
  * def envJwtToken = Java.type('fixtures.EnvConfig')
  * def jwtToken = envJwtToken.get('JWT_TOKEN')
  * def parsingMarketableLocationIds = Java.type("fixtures.MLocationCreateSheetParser")
  * def getMarketableLocationIds = parsingMarketableLocationIds.convertExcelToJson("target/test-classes/location-to-create.xlsx")
  * def filepath = 'file:<absolute Path(target)>'
@Test11
Scenario: Listing successfully created for an available location
  Given url ServicesBaseUrl
  And path 'location', 'create'
  And header Content-Type = 'multipart/form-data'
  And cookie dev-authorization = jwtToken
  * karate.log('Location Ids: ', getLocationIds)

  * 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
发布评论

评论列表(0)

  1. 暂无评论