Ready API - Writing the output to datasink in the same excel file
Hello,
I have scenario in which I need to write the response in the excel sheet. I get two responses and both should be written in the same excel file.
Test steps are as follows :
1) DataSource - This is an unique identifier required in my request and I am using DataGenerator for it.
2) Request1 - This is the request which has an product item number and quantity as well as unique identifier. It gives me the response as OrderNo
3) Request2 :This is the request which has an another product item number and quantity as well as unique identifier. It gives me the response as another OrderNo
4) Property Transfer :- I have created 2 properties for this First Order and Second Order and transfer it to the data sink
4) FisrtDataSink :- I am using the excel file and getting the FirstOrder response in the excel with the cell start at A1
5)SecondDataSink I am using the excel file and getting the FirstOrder response in the excel with the cell start at A2
But the thing it only prints second order response in A2 and A1 is blank. It seems that it is writing the last data sink value.
I need to write both the values one in A1 and another in A2. Is there any way to do it ?
Regards,
Nimish
Hey, thought I answered to this on Friday evening but the answer was somehow lost... :smileysad: I also attached then the demo SoapUI project but I don't have it anymore. I only found a screenshot I took and the code (which was luckily saved in my clipboard manager)...
So the point of my solution is to name all your requests from where you want to extract the customerPo/documentNumber in a certain way. Note that all mine where named 'GetOrder_*'
Then in the groovy script I would filter for those requests, get all the required data and then finally write it to an existing excel:
import java.io.File; import java.io.IOException; import jxl.*; import jxl.write.*; // Define the customerPo and documentNumber variables def customerPo = '' def documentNumber = '' def requestCounter = 0 // Get a list of all test steps in the current test case def testSteps = context.testCase.getTestStepList() // Loop through all the test steps in the current test case // If a test step respects a certain naming then extract the response from it // and register the customerPo and documentNumber in an excel testSteps.each{ if (it.name.startsWith("GetOrder_")) { /** * Extract the customerPo and documentNumber from current request */ def rawResponse = testRunner.testCase.testSteps[ it.name ].testRequest.response.contentAsString // Extract customerPo def customerPoStartIndex = rawResponse.indexOf("<customerPO>") + "<customerPO>".length() customerPo = rawResponse.substring( customerPoStartIndex, rawResponse.indexOf( "</", customerPoStartIndex ) ) // Extract documentNumber def documentNumberStartIndex = rawResponse.indexOf("<documentNumber>") + "<documentNumber>".length() documentNumber = rawResponse.substring( documentNumberStartIndex, rawResponse.indexOf( "</", documentNumberStartIndex ) ) /** * Write to file */ Workbook workbook = Workbook.getWorkbook(new File("D:\\test3.xls")); WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File("D:\\test3.xls"), workbook); WritableSheet writableSheet = writableWorkbook.getSheet(0); // Write the first colummn Label label = new Label( 0, requestCounter, customerPo ); writableSheet.addCell(label); // Write the second colummn Label label2 = new Label( 1, requestCounter, documentNumber ); writableSheet.addCell(label2); //Write and close the workbook writableWorkbook.write(); writableWorkbook.close(); requestCounter++ } }
One final thing though, the excel file needs to be saved as xls (that is as an excel 2003 file).
Cheers!
:smileylol:
Like this... ?
<customerPO>${DataSource#customerPO}-${=testRunner.testCase.testSteps['DataSource'].currentRow}</customerPO>