Forum Discussion

Kevin_Mannering's avatar
Kevin_Mannering
Occasional Contributor
17 years ago

Writing a new file from datasink for each iteration instead of appending

Hi,

I am a greenhorn soapui pro user, trying to save each response to a request in a separate file.I can append all the results in a single file, but I am running about 100 paramaterised requests and would like to save 100 datasink files.

This probably works somehow with groovy, but I have spent the afternoon trying to hack it without sucess and promise i have read all the docu.

Have this code so far, but I can't see how to write the response:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder( "Erstbereitstellung ISP0 --> Aufruf 1#Response" )

log.info( holder.getNodeValue( "//ns1:RequestId" ))

holder.setNodeValue( "//ns1:SubscriptionId", ""+Math.random() )

file = new File("D:captured-request.xml")

So how can I write the holder to a file??

Tnks, lugboy

2 Replies

  • You can use the holders getXml() method to get the String representation fo the xml, and then write that to the file e.g.

    def response = holder.getXml()
    file << response

    Note: the above code will append the response to the file if it already exists, so you might want to ensure that it is empty.

    If the you want a new file for each iteration, use a random number in the name of the file, or the number of the iteration.

    BTW, to ensure prompt response, please post in the SoapUI Pro forum

    Good luck!
    /Nenad
    http://eviware.com
  • Hello,

    I use two Groovy script files, both included here.  The first reads a csv file of properties and updates a Test Suite property variable that a subsequent script with evaluate...  That subsequent script writes to a new file...

    Read a .csv file Groovy script...

    def lineIndx = 0
    def tmpIndx = Integer.parseInt(testRunner.testCase.testSuite.properties["globalIndx"].value)
    def inFileData = 'H:/DATA/SoapUI Projects/Test Data Functionality/DEV Benefit BE Input.csv'
    inFileData = 'C:/SoapUI/DataIn/DEV Benefit BE Input.csv'

    new File(inFileData).splitEachLine(',')
      {
          fields ->
            if (lineIndx == tmpIndx)
            {
                def propBaseRead = testRunner.testCase.getTestStepByName( "Properties In" );
                propBaseRead.setPropertyValue("UserId", fields[1]);
                log.info("=======");
                log.info("dT = " + fields[0] + " ### " + fields[1] + " ### " + fields[2] );
            }
            lineIndx++
      }
    tmpIndx++
    testRunner.testCase.testSuite.properties["globalIndx"].value = tmpIndx

    In between these two scripts are:
    Properties In  --> holds properties from read in .csv file
    Test Request  --> use the properties in a request
    Property Transfer  --> transfer properties from invoked request response as needed
    Properties Out  -->properties from invoked request response as needed


    Write a response file Groovy script...

    // get holder for response...
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder( "Request getMembershipInstances#Response" )
    log.info(holder.xml);
    def userIdInStep = testRunner.testCase.getTestStepByName( "Properties In" );
    def userIdStr = userIdInStep.getPropertyValue( "UserId" );
    // Output the response to a file...
    //new File('c:/temp/' + userIdStr + '.txt').write(holder.prettyXml)
    new File('C:/SoapUI/DataOut/DEV ' + userIdStr + '.txt').write(holder.prettyXml)

    def tmpIndx = Integer.parseInt(testRunner.testCase.testSuite.properties["globalIndx"].value)
    def tmpCount = Integer.parseInt(testRunner.testCase.testSuite.properties["globalCount"].value)
    //log.info("=======");
    //log.info(" = " + tmpIndx + " ### " + tmpCount );
    if (tmpIndx >= tmpCount)
    {
      //  testRunner.gotoStepByName( "Groovy Script Exit" );
      log.info("= End of Run =");
    }
    else
    {
      // go back to the beginning of test case steps to step named  "Groovy Script" to loop again...
      testRunner.gotoStepByName( "Groovy Script" );
    }


    Regards,
    Todd