plaidshirt
plaidshirt

Reputation: 5671

Save attachments automatically in SoapUI with Groovy

I try to save all the attachments from a SOAP response. I use following Groovy script.

def testStep = testRunner.testCase.getTestStepByName("SubmitFile")
def response = testStep.testRequest.response
assert null != response, "response is null"
def outFile = new FileOutputStream(new File(System.getProperty('java.io.tmpdir')+'/test.zip'))
for(i=0; i<3; i++){
    def ins =  response.responseAttachments[0].inputStream
    if (ins) {
       com.eviware.soapui.support.Tools.writeAll(outFile, ins)
    }
}
ins.close()
outFile.close()

I get following error message:

No such property : responseAttachments for class

Upvotes: 0

Views: 875

Answers (3)

Isabel Cenamor
Isabel Cenamor

Reputation: 9

def testCaseName = 'Messages-IP-Mail'

def testStepName = '001_1ConventMailToPDF'

//context.testCase.testSuite.testCases[testCaseName].testSteps[testStepName].testRequest.responseContent

def projectDir = context.expand('${projectDir}');

log.info "Current dir:" + projectDir

def response = context.testCase.testSuite.testCases[testCaseName].testSteps[testStepName].testRequest.response.getAttachments()

def fileName = projectDir + '/pdf.pdf'

def outFile = new FileOutputStream(new File(fileName))

testRunner.testCase.testSteps["fileName"].setPropertyValue("fileName", fileName)

def ins = response[0].inputStream

if (ins) {

       com.eviware.soapui.support.Tools.writeAll(outFile, ins)
    }
ins.close()
outFile.close()

Upvotes: 1

Learn Smarter
Learn Smarter

Reputation: 26

Use following script in assertion (Script assertion) in Soap UI.

def response = messageExchange.response
assert null != response, "response is empty"

def outFile
def inputStream
    if(messageExchange.responseAttachments.size() >0){
            inputStream =  messageExchange.responseAttachments[0].inputStream
        if (inputStream) {
              outFile = new FileOutputStream(new File('/Users/sys/Documents/test/bank_response.txt'))
           com.eviware.soapui.support.Tools.writeAll(outFile, inputStream)

        }
    }else{
        log.error 'No attachments found!!!'
    }

if(inputStream) inputStream.close()
if(outFile) outFile.close()

Upvotes: 1

craigcaulfield
craigcaulfield

Reputation: 3538

responseAttachments is a property on MessageExchange. You're using Response, so you'd need attachments instead. See the API docs for more details.

Upvotes: 1

Related Questions