如何使用groovy从soap响应中导出pdf附件?

时间:2016-09-21 14:32:20

标签: groovy soapui

虽然 soap (免费版)可以选择导出响应中生成的文档。是否有任何groovy函数来提取application / pdf文件并存储在我的本地文件夹中?

enter image description here

1 个答案:

答案 0 :(得分:1)

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

将以下脚本Script Assertion添加到当前请求步骤。内联查找相应的评论。

脚本的来源取自here

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