0
votes

I am writing a groovy script where I am extracting text from a response and writing it to an output file on my system. The problem I encounter is when I run the groovy script that calls the test. The script assertions runs and logs the text twice to the file.

It seems to write to the file just before it leaves the assertion.

I have tried the following:

...
...
if (context.alreadyWritten == null || !context.alreadyWritten) {
    inputFile.append (testString+ "\n")
    log.info testString
} else {
   log.info ('Already written!')
}   

I have set the flag (context.alreadyWritten) to false in groovy before I execute the test step.

SOAPUI version : 5.3.0

I see there was an issue previous with Smartbear when appending to file in an assertion script. However the workaround was advised to resolve this:

if (context.alreadyWritten == null || !context.alreadyWritten) {
}

Which does not resolve my issue

When I log the result using log.info I see only one instance of the message been logged.

Any ideas.

Thanks

1
DO you just need to write the response to the file. Is that all? - Rao

1 Answers

0
votes

If you are using Script Assertion, try below:

//Define the file name, change as needed
def fileName = '/path/to/file.xml'

//check if you got the response

if (context.response) {
   log.info 'Response available and not empty'
   def file = new File(fileName)
   if (!context?.alreadySaved) {
       file.write(context.response)
       context.alreadySaved = true
       log.info 'response written to file'
   } else {
     log.info 'Response already written'
   }
} else {
  log.info 'there is no response to save'
}