Forum Discussion

nimishbhuta's avatar
nimishbhuta
Frequent Contributor
7 years ago
Solved

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) D...
  • Lucian's avatar
    Lucian
    7 years ago

    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!

  • Lucian's avatar
    Lucian
    6 years ago

    :smileylol:

     

    Like this... ?

     

    <customerPO>${DataSource#customerPO}-${=testRunner.testCase.testSteps['DataSource'].currentRow}</customerPO>