使用 Groovy 在 SoapUI 中自动保存附件

Save attachments automatically in SoapUI with Groovy

我尝试保存 SOAP 回复中的所有附件。我使用以下 Groovy 脚本。

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()

我收到以下错误消息:

No such property : responseAttachments for class

responseAttachmentsMessageExchange 上的 属性。您正在使用 Response,因此您需要 attachments。有关详细信息,请参阅 API docs

在 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()
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()