Forum Discussion
HI PrathapR,
Thank you for your post. Here are my comments:
1. There is a feature request to export the data to an Excel file along with the property names:
2.
PrathapR wrote:
1) When added Template File
There is a bug with using multiple DataSink test steps. If you have 2 test cases and run the TestSuite, ReadyAPI will write the data only for one DataSink (selected randomly).
3.
2) When we don't add Template file and Checked "Append to end of file option"
You can use a groovy script to add property names to a file. Note that, in this case, you will need to enable the "Append to end of file" option and specify the Worksheet name in the DataSink test step options.
Here is the script example (feel free to optimize):
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; String excelFilePath = "D:\\Response.xlsx"; //Provide a path to your output Excel file FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); //Specify a sheet by index (starts from zero) Row row = sheet.createRow(0); int columnCount = 0; Cell cell = row.createCell(columnCount); def data = context.testCase.getTestStepByName("DataSink"); //Provide your DataSink test step name propToGet = data.getPropertyNames(); for ( int j = 0 ; j < propToGet.size(); j++ ){ def name = propToGet[j] log.info name cell.setCellValue(name); cell = row.createCell(++columnCount); } inputStream.close(); FileOutputStream outputStream = new FileOutputStream(excelFilePath); workbook.write(outputStream); workbook.close(); outputStream.close();
Don't forget to change the values in bold. In particular, a sheet index (or name) should be different for different test cases (since you use different sheets for them).
Also, please note that the script works starting from ReadyAPI 3.0.0.
- PrathapR5 years agoFrequent Contributor
Thanks for reply Nastya_Khovrina .
1)Yes, I can able to do with Apache POI with Groovy script. But would like to use data sink steps, since using paid version.
2) Yesterday had a call with SmatBear system engineer and opened a ticket for Data Sink steps.
- HimanshuTayal5 years agoCommunity Hero
PrathapR : You can use the Groovy Code to write the data into any particular Cell but i would suggest not to use same Excel sheet while reading and writing because
what i have observed with working on same excel workbook is :
Excel file got corrupted if object didn't get closed by your code(anything script failure, run time issue, etc etc) and you try to read the file.
- sonya_m5 years agoSmartBear Alumni (Retired)
Thank you Himanshu!!
PrathapR Please keep us updated on the investigation results with the support team.
- sonya_m5 years agoSmartBear Alumni (Retired)
PrathapR , I see that you got a temporary solution in the support ticket. Let me post it here and mark it so, as well.
Do not hesitate to update this thread!
>>
The only way I see out of the box to add headers when creating a new sheet in a excel file is to use a template file. The template file will be written to the Output File in the exact format it is in so the template file should be the same file as the output file. The thing that would need to change dynamically is the "Start at cell" attribute. If you save the last row the output excel file was written to a testsuite property, increment it, and then use dynamic property expansion to retrieve it in the "Start at cell" attribute the new values will be written to the correct row each time. Below is an example of a dynamic property expansion that can be used in the "Start at cell" attribute.
${= return 'A' + context.expand( '${#TestSuite#rowNum}' )}
If the property "rowNum" value is 3 , the dynamic property expansion will return A3 as the "Start at cell" value and data will be written starting at cell A3. Below is a link on dynamic property expansion and how to save values to properties using Groovy.
https://support.smartbear.com/readyapi/docs/testing/properties/expansion.html#dynamic
https://support.smartbear.com/readyapi/docs/testing/scripts/samples/variable.html#get-and-set-properties<<
Related Content
- 2 years ago
- 6 months ago
- 13 years ago
- 2 years ago
Recent Discussions
- 22 days ago