Search code examples
groovysoapui

Fetch a value from a response and write into a file


I'm new to groovy in soapui. I am trying to fetch a value from a mockservice response tag and store that in a text file. Then another service will read that file to use in a request.

//Response tag from a mockresponse of a service i.e. service1 <ref:ReferState RequestID = "0004350758">

//store in a file so that the request from another service can use this ID

//Request from another service i.e. service2 <ref:ReferState RequestID = "0004350758">

And this Request ID value is incrementing every run.

Can someone help please with the groovy code? many thanks in advance.


Solution

  • You set up a Groovy Test Step to run after your service call (assuming it's SOAP).

    import com.eviware.soapui.support.XmlHolder
    
    def xml = new XmlHolder(context.response)
    
    //getting response value with xpath, this you will have to figure out yourself depending on the XML structure:
    def responseValue = xml.getNodeValue("//*:ReferState/*: RequestID") 
    log.info "responseValue : ${responseValue}"
    
    def outFile = new File("/your/path/and/filename.txt")
    outFile.write(responseValue)
    

    And then in the other project, you can access the value as:

    def inputValue = new File("/your/path/and/filename.txt").text
    log.info "inputValue : ${inputValue}"
    

    If you want to use that value in a new service call, put it in a property:

    testRunner.testCase.setPropertyValue("RequestID", "responseValue")
    

    and in your next request parameterise it:

    <soap:Header/>
        <soap:Body>
            <ns:SomeRequest>
                 <ref:RequestID>${#TestCase#responseValue}</ns:address>
                 ...
    

    Coincidentally, SoapUI also has global propeties that are visible across projects:

    Setting:

    com.eviware.soapui.SoapUI.globalProperties.setPropertyValue("GlobalPropName", "PropValue")
    

    Getting

    com.eviware.soapui.SoapUI.globalProperties.getPropertyValue("GlobalPropName") == "PropValue"