且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Groovy从Soap响应中导出pdf附件?

更新时间:2022-10-31 13:30:20

以下脚本应该能够将附件保存到文件中.

The following script should be able to save the attachment to a file.

将以下脚本作为Script Assertion添加到当前请求步骤.内联找到适当的注释.

Add the below script as Script Assertion to the current request step. Find the appropriate comments inline.

该脚本的来源来自此处

/**
* Below script assertion will check 
* if the response is not null
* there is an attachment
* and saves file to the specified location, by default saves into system temp
* directory
**/
//change file name as needed
def fileName = System.getProperty('java.io.tmpdir')+'/test.pdf'

//Get the response and check if the response is not null
def response = messageExchange.response
assert null != response, "response is null"

//Create output stream
def outFile = new FileOutputStream(new File(fileName))

//Check if there is one attachment in the response
assert 1 == messageExchange.responseAttachments.length, "Response attachments count not matching"
def ins = messageExchange.responseAttachments[0]?.inputStream
if (ins) {
   //Save to file
   com.eviware.soapui.support.Tools.writeAll( outFile,  ins )
}
ins.close()
outFile.close()