Forum Discussion

morciani's avatar
morciani
Occasional Contributor
14 years ago

data driven Test Case - saving requests / responses to an external folder

Hi All,

I've built a data driven test case that works nicely.  It looks like this:

Test Case1
1. Properties (global data)
2. DataSource (request specific data from an Excel sheet)
3. DataGen (some data that needs to be calculated on the fly)
4. TestRequest (with a handful of assertions)
5. Groovy Script (logs results and transfers some properties from the response to the following step)
6. Properties_Response (holds properties from the latest response)
7. DataSource Loop (keeps submitting requests until it runs out of data)

Pretty standard, and it works well, but I'd like it to do a bit more.  I would like to be able to collect the actual requests sent and responses received in an external location ('c:/soapUI_requests_responses' for example).  Take this example:

My Excel sheet in Step 2 contains the following 3 requests (rows):

01_AK_001
02_AL_001
03_AR_001

If run with this data, my Test Case will iterate 3 times.  I would like a way to save the requests / responses externally, so that 'c:/soapUI_requests_responses' contains the actual requests / responses passed to the service, like this:

01_AK_001_Request.xml
01_AK_001_Response.xml
02_AL_001_Request.xml
02_AL_001_Response.xml
03_AR_001_Request.xml
03_AR_001_Response.xml

Any ideas on some Groovy scripting (or other means) that would accomplish this?  Thanks in advance for any help!

-Mike

7 Replies

  • Hiya,

    How about a groovy script along the following lines:


    def name = context.expand( '${Properties#Filename}' )
    def location = context.expand( '${Properties#Location}' )

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

    def getRequestFilename(name) { 
    reqFilename = shortDate + "-" + name + "-request.xml"
    }

    def file = new PrintWriter (location + getResponseFilename(name))
    def Response = testRunner.testCase.testSteps["Request"].testRequest.response.contentAsString
    file.println(Response)
    file.flush()
    file.close()

    def file2 = new PrintWriter (location + getRequestFilename(name))
    def Request = context.expand( '${Request#Request#//soapenv:Envelope[1]}' )
    file2.println(Request)
    file2.flush()
    file2.close()


    This allows you to determine the folder you want to save in a property, and the same for the root part of the filename (in your case 01_AK_001, 02_AL_001...) - you can read this into the appropriate property from elsewhere (or you could read it in directly here and bypass the Property completely if you want).
    It adds a datetime stamp so if you run the same request multiple times the requests and responses don't overwrite each other.
  • morciani's avatar
    morciani
    Occasional Contributor
    Excellent!!!!  Thank you!!!  I REALLY appreciate the time you devoted to this.  I had something similar, but I didn't have the file.flush() or file.close() references.  This  works great.  the final version looks like this:

    //Output the request and response to a local directory

    def name = context.expand( '${DataGen#CompanysQuoteNumber}' )
    def location = context.expand( '${Properties#Location}' )

    def getResponseFilename(name) {
      date = new Date()
      dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd_HH.mm.ss")
      shortDate = dateFormat.format(date)
      respFilename = name + "_Rs_" + shortDate + ".xml"
    }

    def getRequestFilename(name) { 
      reqFilename = name + "_Rq_" + shortDate + ".xml"
    }
     
    def file = new PrintWriter (location + getResponseFilename(name))
    def Response = testRunner.testCase.testSteps["TestRequest_TPPR"].testRequest.response.contentAsString
    file.println(Response)
    file.flush()
    file.close()

    def file2 = new PrintWriter (location + getRequestFilename(name))
    def Request = context.expand( '${TestRequest_TPPR#Request}' )

    file2.println(Request)
    file2.flush()
    file2.close()
  • Hi All,

    I am unable to save contents of request & response as XML file when test suite runs on iteration, I tried this code;
    def name = testRunner.testCase.name
    def location = "E:\\MPI\\Integration\\Paramterization\\Release 15\\"

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

    //Response File//
    def Responsefile = new PrintWriter (location + getResponseFilename(name))
    def Response = context.expand( '${executeBO - GetJobDetails#Response}' )
    Responsefile.println(Response)
    Responsefile.flush()
    Responsefile.close()

    //Request File//
    def getRequestFilename(name) {
    reqFilename = shortDate + "-" + name + "-request.xml"
    }
    def Requestfile = new PrintWriter (location + getRequestFilename(name))
    def Request = context.expand( '${executeBO - GetJobDetails#Request}' )
    Requestfile.println(Request)
    Requestfile.flush()
    Requestfile.close()

    What is happening is its saving empty files in the specified location on each iteration. Any help on this would be of great help.

    Regards
  • Hello,

    I'm a new SOAPUI PRO user (for some reason I can’t Post or Reply in the dedicated Forum).

    I try to save responses (including attachments) to an external folder and unfortunately I can't succeed adapting the "groovy scripts" I found in the Forum, or with the parameters (TestRequestProperties etc…).

    • Requirements are:
    - Automatically export Xml response in a dedicated Folder (that's work!)
    - Automatically export response attachments in a dedicated Folder (that don’t work :/)

    • Request :
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://xxxxxxx/">
    <soapenv:Header/>
    <soapenv:Body>
    <ws:recevoirCr_Demande>
    <codeCollecteur>JDC</codeCollecteur>
    <motDePasse>JDC01</motDePasse>
    <versionDadsu>V01X04</versionDadsu>
    <crNouveau>OUI</crNouveau>
    <crDemandeArray/>
    </ws:recevoirCr_Demande>
    </soapenv:Body>
    </soapenv:Envelope>

    • Response :
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:recevoirCr_Reponse xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:ns2="http://xxxxxx/">
    <envCrRetourneArray>
    <envCrRetourne>
    <idEnvDadsu>DAD_JDC_001000000000000000000000000000000006</idEnvDadsu>
    <nomEnveloppeCr>CR_DAD_JDC_001000000000000000000000000000000006_001_CR2.zip</nomEnveloppeCr>
    <fichier>
    <Include href="cid:1998f71c-c8e9-4645-a14c-16ea092bb1dd@example.jaxws.sun.com" xmlns="http://www.w3.org/2004/08/xop/include"/>
    </fichier>
    </envCrRetourne>
    <envCrRetourne>
    <idEnvDadsu>DAD_JDC_001000000000000000000000000000000002</idEnvDadsu>
    <nomEnveloppeCr>CR_DAD_JDC_001000000000000000000000000000000002_001_CR2.zip</nomEnveloppeCr>
    <fichier>
    <Include href="cid:fe53e511-3d37-4069-8ddf-51c345a9d7ae@example.jaxws.sun.com" xmlns="http://www.w3.org/2004/08/xop/include"/>
    </fichier>
    </envCrRetourne>
    </envCrRetourneArray>
    <crIndisponibleArray/>
    <codeRetour>00</codeRetour>
    <libelleRetour>Prise en compte correcte</libelleRetour>
    </ns2:recevoirCr_Reponse>
    </S:Body>
    </S:Envelope>

    • You can find 1 to x attachments in the response, the attachment is always a .zip file.

    • The idea is :
    1. Each time I run the testcase or testsuite to create a folder named: “AAAAMMDD HHMMSS”-“name of the testcase”
    2. In this folder simply save all the attachments (the filename is the response: “nomEnveloppeCr” propertie).

    Could you help me resolve that?

    Looking forward to hearing from you

    Regards

    Pierre
  • Ka1's avatar
    Ka1
    New Member

    Hi All -

     

      I want to store my request and response to a file without any scripting.Can anyone help me plz

    • nmrao's avatar
      nmrao
      Champion Level 3
      Ka1, suggest you to create a new thread with the details of the issue being faced.