0

I am able to create XSSFWorkbook() in the first iteration only. In second iteration, JSR233 Sampler is passing but Workbook is not getting created.

Workbook wb = new XSSFWorkbook();

Sheet sheet = wb.createSheet("SiteList"); Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow(0);

Create a cell and put a value in it. row.createCell(0).setCellValue('ErtStudyName'); row.createCell(1).setCellValue('UniqueSiteID'); row.createCell(2).setCellValue('SiteNumber'); row.createCell(3).setCellValue('InvestigatorFirstName'); row.createCell(4).setCellValue('InvestigatorLastName'); row.createCell(5).setCellValue('InvestigatorMiddleName'); row.createCell(6).setCellValue('ShippingAddress1'); row.createCell(7).setCellValue('ShippingAddress2'); row.createCell(8).setCellValue('ShippingAddress3'); row.createCell(9).setCellValue('ShippingAddress4'); row.createCell(10).setCellValue('InstitutionName'); row.createCell(11).setCellValue('City'); row.createCell(12).setCellValue('PostalCode'); row.createCell(13).setCellValue('Country'); row.createCell(14).setCellValue('Region'); row.createCell(15).setCellValue('ShippingPhone'); row.createCell(16).setCellValue('ShippingPhoneExtension'); row.createCell(17).setCellValue('ShippingFax'); row.createCell(18).setCellValue('SiteAction');

row = sheet.createRow(1); row.createCell(0).setCellValue('${studyname}'); row.createCell(1).setCellValue('Uniques_${id2}'); row.createCell(2).setCellValue('${id2}'); row.createCell(3).setCellValue('InvestigatorFirstName1'); row.createCell(4).setCellValue('LAstName1'); //row.createCell(5).setCellValue('InvestigatorMiddleName');-- row.createCell(6).setCellValue('adrres1'); row.createCell(7).setCellValue('adrres2'); //row.createCell(8).setCellValue('ShippingAddress3'); //row.createCell(9).setCellValue('ShippingAddress4'); row.createCell(10).setCellValue('Institution'); row.createCell(11).setCellValue('Newyork'); row.createCell(12).setCellValue(32133); row.createCell(13).setCellValue('India'); row.createCell(14).setCellValue('NYC'); row.createCell(15).setCellValue('312312312'); row.createCell(16).setCellValue('312312312'); //row.createCell(17).setCellValue('ShippingFax'); row.createCell(18).setCellValue('Create');

Write the output to a file

try (OutputStream fileOut = new FileOutputStream("C:/Users/saurabh.arora/Desktop/Performance/SiteService/SiteUpload/testdata/mainint/workbook${__threadNum}${__iterationNum}.xlsx")) { wb.write(fileOut); }

to create a new workbook in new iteration

1 Answers1

0

This is your problem:

${__threadNum}${__iterationNum}

As per JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

  • Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
  • Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

Your code works fine but 2nd iteration simply overwrites the file created in 1st iteration as the functions have been resolved to values, compiled and cached.

So replace:

  • ${__threadNum} with ctx.getThreadNum()
  • ${__iterationNum} with vars.getIteration()

Where:

More information on the above and other JMeter API shorthands available for JSR223 Test Elements: Top 8 JMeter Java Classes You Should Be Using with Groovy

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • It is able to replace the values in 2nd iteration OutputStream fileOut = new FileOutputStream("C:/Users/saurabh.arora/Desktop/Performance/SiteService/SiteUpload/testdata/mainint/workbook12.xlsx") wb.write(fileOut); but workbook is not getting created – Saurabh Aug 29 '23 at 12:43