Forum Discussion

Department_of_T's avatar
Department_of_T
Contributor
12 years ago

Write XML response to a file

Hi, I have the following groovy code which successfully writes the response files to disk but it always writes at least 2 copies of the request and 2 of the response instead of just one. Can anyone advise on how to only save the request/response once?
Using responseFile.write(response) or responseFile.println(response) does not make a difference.

groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def name = context.getCurrentStep().getLabel()
def testSuiteLongName = messageExchange.modelItem.testStep.testCase.testSuite.name
def testCaseLongName = messageExchange.modelItem.testCase.name
def testSuite = testSuiteLongName.replace("TestSuite: ", "")
def testCase = testCaseLongName.replace("TestCase: ", "")
def testStep = messageExchange.modelItem.testStep.name

def directory = "C:/SoapOutput/" + date() + "/" + testSuite+ "/" + testCase
def failedDirectory = "C:/SoapOutput/" + date() + "/" + testSuite+ "/" + testCase + "/Failures"
new File(directory).mkdirs()
def row = messageExchange.modelItem.testCase.testSteps["FindPersonValidations DataSource"].currentRow
def inputRowNo = messageExchange.modelItem.testCase.testSuite.getPropertyValue("input")
def inputReasonCode = messageExchange.modelItem.testCase.testSuite.getPropertyValue("reasoncode")

def time() {
date = new Date()
dateFormat = new java.text.SimpleDateFormat('HH-mm-ss-SSS')
shortDateName = dateFormat.format(date)
return shortDateName
}

def date() {
date = new Date()
dateFormat = new java.text.SimpleDateFormat('ddMMyyyy')
shortDateName = dateFormat.format(date)
return shortDateName
}

//Save Request File
def requestFile = new PrintWriter ( directory +"/"+time()+" "+ name + " [request_row"+inputRowNo+" ]("+ inputReasonCode + ").xml")
def request = context.expand( '${'+ name + '#Request}' )
requestFile.println(request)
requestFile.flush()
requestFile.close()

//Save Response File
def responseFile = new PrintWriter (directory +"/"+ time()+" "+ name +" [RESPONSE_row"+inputRowNo+" ]("+ inputReasonCode + ").xml")
def response = context.expand( '${' +name + '#Response}' )
responseFile.write(response)


OUTPUT of just clicking the PLAY button to test the groovy script.
09-01-37-475 findPersonChangeCustomerDetails - XML [request_row8 ](subsequent_given_names Invalid Format)
09-01-37-481 findPersonChangeCustomerDetails - XML [RESPONSE_row8 ](subsequent_given_names Invalid Format)
09-01-37-482 findPersonChangeCustomerDetails - XML [request_row8 ](subsequent_given_names Invalid Format)
09-01-37-488 findPersonChangeCustomerDetails - XML [RESPONSE_row8 ](subsequent_given_names Invalid Format)

note: I need the timestamp in milliseconds so i can sort the files nicely in Windows Explorer by name (otherwise there can be 2 or 3 requests sent in 1 sec which show up out of order when sorting the files)

5 Replies

  • nmrao's avatar
    nmrao
    Icon for Champion Level 1 rankChampion Level 1
    Are you sure this script is not called twice?
  • Yes, i am sure its not called twice. But to test the script, if you click the green play button in the groovy script window ONCE then 2x requests/responses are still written to file.

    I have seen this script work in other examples on here but the timestamp does not use milliseconds. If i change the timestamp from HH-mm-ss-SSS to HH-mm-ss then only 1 request/response is written which i assume is because the file is just overwritten twice (same name)
  • I have the code as a 'script assertion' in the XML file. I have now changed this to an event handler but i cannot see any logs. I am attempting to use dates/times as a directory structure do to the sheer volume of tests that we run. Can anyone see a problem with the below event handler?

    //new File(directory).mkdirs() 

    def time() {
    date = new Date()
    dateFormat = new java.text.SimpleDateFormat('HH-mm-ss-SSS')
    shortDateName = dateFormat.format(date)
    return shortDateName
    }

    def date() {
    date = new Date()
    dateFormat = new java.text.SimpleDateFormat('ddMMyyyy')
    shortDateName = dateFormat.format(date)
    return shortDateName
    }
    def directory = "C:/SoapOutput/" + date() + "/"+ time()

    fos = new FileOutputStream( directory + testStepResult.testStep.label + '.txt', true )
    pw = new PrintWriter( fos )
    testStepResult.writeTo( pw )
    pw.close()
    fos.close()
  • Hi,

    you can't see any logs since there's nothing written to: log.info "output string here"

    One potential error I see here is you reference a date/time directory without creating it. I'd double check that in "error log" tab as well at the bottom of SoapUI Pro.

    Thanks,
    Michael Giller
    SmartBear Software