Forum Discussion

akj504's avatar
akj504
New Contributor
16 years ago

howto save response while saving SoapUI project

Hi All,

I am using SoapUI 2.5.1 - evaluation version
In a testcase, how to save the response messages along with requests in a SoapUI project

When I create a Web service SoapUI project and send soap request for an operation, I get a response XML. Now when saving the soapUI and exiting the tool - when once again I open I see the response XML tab as empty.

Why only request gets saved and not responses - How to save responses also.

Thanks
-Arun
  • akj504's avatar
    akj504
    New Contributor
    Is it that no one knows how to this simple thing in SoapUI

    anybody... pls reply
  • Lens's avatar
    Lens
    Occasional Contributor
    Hi Arun,

    You can use following script to save your response to a xml-file.

    def File = New PrintWriter ("C:/.../response.xml")
    def Response = testRunner.testCase.testSteps["aaa"].testRequest.response.contentAsString
    File.println(Response)
    File.flush()
    File.close()

    "aaa" reflect to the name of your test step Test Request.

    Regards,
    Benny
  • Hi Arun and Lens,


    The code snippet from Lens is pretty much spot on with a slight change that new operator in Groovy (just like in Java and many other object oriented languages) should be lower case. Apart from that, I would also suggest using lowercase first letters in variables as well as other Sun's Java naming conventions since they have been widely accepted. In additional, using them will spare you from being identified as Visual Basic programmer. In this case, File variable could be confused with class with the same name and capitalization.

    I would propose an improvement to solution Lens which logs response into a file with a generated filename that includes time stamp (so that all responses can be sorted chronologically). Creating a se
    Here's the snippet of a utility method logResponseToFile(testStepName) with my humble corrections:

    def getResponseFilename(name) {
    date = new Date()
    dateFormat = new java.text.SimpleDateFormat('yyyyMMdd-kkmmss')
    shortDate = dateFormat.format(date)
    respFilename = shortDate + "-" + name + "-response.xml"  // implicitely returned
    }

    def saveResponseAsFile(testStepName) {
    def file = new PrintWriter (getResponseFilename(testStepName))
    if (log.isInfoEnabled())
    log.info("Opening response file: " + respFilename)
    def response = testRunner.testCase.testSteps[testStepName].testRequest.response.contentAsString
    file.println(response)
    file.flush()
    file.close()
    if (log.isInfoEnabled())
    log.info("Saved response in file: " + respFilename)
    }

    logResponseToFile("My test step name")


    Have in mind that using pro version of soapUI you can put the corresponding Groovy script into $SOAPUI_HOME/bin/scripts and have this neat functionality available in any soapUI project. In that case the only line in your Groovy script is this:

    soapui.utils.LogUtils.logResponseToFile("My test step name", testRunner, log)


    If the supplied test case in not found, an error will be reported without disrupting soapUI tests being run.

    Future pro versions of soapUI will contain improved version of this Groovy script.


    Cheers!
    /Nenad Nikolic a.k.a. Shonzilla
  • coexmatrix's avatar
    coexmatrix
    Occasional Contributor
    I am trying to use your example as follows:
    def getResponseFilename(name) {
    date = new Date()
    dateFormat = new java.text.SimpleDateFormat('yyyyMMdd-kkmmss')
    shortDate = dateFormat.format(date)
    respFilename = shortDate + "-" + name + "-response.xml" // implicitely returned
    }

    def saveResponseAsFile(findItemsAdvanced) {
    def file = new PrintWriter (getResponseFilename(findItemsAdvanced))
    if (log.isInfoEnabled())
    log.info("Opening response file: " + respFilename)
    def response = testRunner.testCase.testSteps[findItemsAdvanced].testRequest.response.contentAsString
    file.println(response)
    file.flush()
    file.close()
    if (log.isInfoEnabled())
    log.info("Saved response in file: " + respFilename)
    }

    logResponseToFile(findItemsAdvanced)

    but I receive the following message: "groovy.lang.MissingPropertyException: No such property: findItemsAdvanced for class: Script19 No such property: findItemsAdvanced for class: Script19 "

    Any help would be greatly appreciated
  • I am also facing problem for saving the response. i am running the following script (m running this script on save scripts at project level)

    def getResponseFilename(name) {
    date = new Date()
    dateFormat = new java.text.SimpleDateFormat('yyyyMMdd-kkmmss')
    shortDate = dateFormat.format(date)
    respFilename = shortDate + "-" + name + "-response.xml" // implicitely returned
    }

    def saveResponseAsFile(testStepName) {
    def file = new PrintWriter (getResponseFilename(testStepName))
    if (log.isInfoEnabled())
    log.info("Opening response file: " + respFilename)
    def response = testRunner.testCase.testSteps[testStepName].testRequest.response.contentAsString
    file.println(response)
    file.flush()
    file.close()
    if (log.isInfoEnabled())
    log.info("Saved response in file: " + respFilename)
    }

    logResponseToFile("Prequilify")

    i am getting error message" groovy.lang.MissingPropertyException: No such property: findItemsAdvanced for class: Script19 No such property: findItemsAdvanced for class: Script19 "


    Can any one help me on this ...its realy urgent
  • It worked for me i just used saveResponseAsFile("TestRequestName") instead of logResponseToFile("TestRequestName") and i put the script under Tear Down Script of the test case.