Groovy Script to save Test Results to a file in soapUI

Are you using soapUI and do you want to find out a way to store your request and response log somewhere in your hard drive dynamically? Well.. the below script should to the job for you.

Add a Groovy step and copy paste the below code and execute your test suite that's it..!! Just make sure to replace the below TestName with the actual Test name.

Groovy Code:

1. Fixed File Name:

//Get Current date time stamp to append at the end of the file name.
def date = new Date()
def dts = date.format("yyyy-MM-dd-HH-mm-ss")

//Write Request to XML File
def myXmlRequest = "C:/Request"+dts+".xml"
def request = context.expand('${TestName#Request}')
def req = new File(myXmlRequest)
req.write(request, "UTF-8")

// Write Response to XMl File
def myXmlResponse = "C:/Response"+dts+".xml"
def response = context.expand('${TestName#Response}')
def res = new File(myXmlResponse)
res.write(response, "UTF-8")


2. Takes Test Case Name as File Name:

import com.eviware.soapui.support.*;
//Get the TestCase Name to use it as a File Name.
def testCase = testRunner.getTestCase()
fileName= testCase.getName()
//Get Current date time stamp to append the same in the file name.
def date = new Date()
def dts = date.format("yyyy-MM-dd-HH-mm-ss")

//Write Request to a XML File and save it with Date Time stamp appended to your filename.
def myXmlRequest = "C:/"+fileName+"Request_"+dts+".xml"
def request = context.expand('${ConversionRate#Request}')
def req = new File(myXmlRequest)
req.write(request, "UTF-8")

// Write Response to a XMl File and save it with Date Time stamp appended to your filename.
def myXmlResponse = "C:/"+fileName+"Response_"+dts+".xml"
def response = context.expand( '${ConversionRate#Response}' )
def res = new File(myXmlResponse)
res.write(response, "UTF-8")


Let me know if this worked for you..!! :)
Groovy Script to save Test Results to a file in soapUI Groovy Script to save Test Results to a file in soapUI Reviewed by Suntaragali The Smart Techie on March 06, 2013 Rating: 5

19 comments:

  1. Hey! I tested your solution, but my files are 0bytes after executing the test. It seems like the files are created via Groovy but the request and response are not saved.

    Any ideas?
    Thanks!

    ReplyDelete
    Replies
    1. Romina,

      Check whether you have provided the correct soap client (TestStep Name) name for which you are trying to save the traffic. Please crosscheck in your code for the below lines.

      def request = context.expand('${ConversionRate#Request}')
      def response = context.expand( '${ConversionRate#Response}' )

      Here in my example, ConversionRate is my SOAP Client Name (Test Step Name).

      Let me know if this helps..!!

      Delete
  2. Thanks. This is great :-)

    ReplyDelete
  3. Hi

    I have used the same script but its not working.My files are 0 bytes after executing the test.

    def request = context.expand('${ConversionRate#Request}')
    def response = context.expand( '${ConversionRate#Response}' )


    ConversionRate - I have used my soap client name
    Request - hope its the keyword i can use as is
    Response - hope its the keyword i can use as is

    Please suggest me is there any other place i need to change.

    ReplyDelete
  4. Hi, I need to get the Request of all the test cases in each testsuite. I am not able to do that, Could you please help me..

    Example : I have two testsuites named TestSuite1, TestSuite2

    Each has testcases in it. now i need to create an another testsuite and extract the request of all the testcases in the other suites one by one.

    def request = context.expand('${ConversionRate#Request}') returning empty as the requested testcase is not in thethird testsuite which i have created for extracting the other suites test case requests.


    Please help!!!!!


    ReplyDelete
  5. Hi,

    I am new to soapui, I am using REST Test Request to insert data to respected columns, but here problem with date column. I am giving date values statically with current date, But whenever these tests will run in future after 2-3 months, these tests will get fail. So can you please suggest me with examples.

    ReplyDelete
  6. Hi

    Fantastic very nice explanation....It helped me ..Thanks indeed. ...

    Thanks,
    uday

    ReplyDelete
  7. Hi,

    Is it possible to only get a part from the response to the log file?
    def response = context.expand( '${ConversionRate#Response}' )

    The above will take everything in the response but if I am only interested in the a specific "tag" in the response? Is it possible to only capture that to the log?

    ReplyDelete
    Replies
    1. I am also intrusted in your response.

      Delete
  8. Hi,

    How do i make the saved filename the name of the downloaded file from the response so that every file saved into the specified location is the name of the downlaoded file?

    ReplyDelete
  9. Hi,
    Can i save only a particular tag from the response to a file and reuse this response data in another test case or project?

    ReplyDelete
  10. hello, I used the above code, but i am getting java.io exception

    ReplyDelete
  11. Hi I run the above code it worked for the specific test step name given in script. But i want it to be executed for all the test steps which are in test suite.
    Please help me how to do that?

    ReplyDelete
  12. Hi,

    How to capture request and response for complete test suite ..

    Please help.

    Thanks
    Sri

    ReplyDelete
  13. Hi

    Could share with us how to export to excel the Testcase properties using a Groovy teststep at the end of the testcase?

    ReplyDelete
  14. too good article, using the same code in my framework with some modifications in naming n all. 100/100 for you.

    ReplyDelete