将SOAP响应下载为xml文件

时间:2014-02-25 09:43:29

标签: xml soap soapui

我有一个客户端从SOAP服务获取内容。因为它使用了大量资源,我需要测试很多东西,我不想弄乱它们的服务器功能。

这就是为什么我需要一种方法将原始XML响应下载到.xml文件。最简单的方法是什么?

我已经尝试使用SOAPUI转储到文件,但这不允许自动化soap调用并按顺序下载所有文件。

2 个答案:

答案 0 :(得分:0)

如果要使用SOAPUI保存所有响应来执行SOAP Test请求的整个测试用例,可以使用以下代码在testCase中添加 groovy脚本步骤作为最后一步:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def tcase = testRunner.testCase
// get total number of testSteps
def countTestSteps = tcase.getTestStepList().size()
// finish with -1 to avoid groovy script testStep
for(i=0;i<countTestSteps-1;i++){
    // define a file
    def file = new File("C:/temp/SOAPUI_response_"+i+".xml")
    def pw = new PrintWriter(file)
    // get testStep
    def testStep = tcase.getTestStepAt(i)
    // get response
    def response = testStep.getProperty('Response').getValue()
    // save on file
    pw.println(response)
    pw.flush()
    pw.close()
}

使用此代码,您的testSteps的所有响应都将保存到光盘(代码注释解释了行为)

此外,如果您想多次运行testCase,避免覆盖生成的文件,您可以使用例如随机生成的UUID来创建文件名,请尝试:

    def fileNamePrefix = UUID.randomUUID().toString()
    def file = new File("C:/temp/" + fileNamePrefix + "SOAPUI_response_"+i+".xml")

而不是

def file = new File("C:/temp/SOAPUI_response_"+i+".xml")

希望这有帮助,

答案 1 :(得分:0)

与albciff建议的类似,但我只想记录特定的请求:

// get a unique value
def date = new Date();
def formatted = date.formatted('MMddss');
// set the base location for my file
def fileBase = "C:/temp/SoapUI/Requests/";
// create the unique file name
def fileName = formatted + "_Foo_Request.xml";
// grab the request, for this my request is "Foo"
def request = context.expand('${Foo#Request}');
// create the file
def f = new File(fileBase + fileName);
// write the request to the file
f.write(request, "UTF-8");

同样可以通过响应来完成。只需更改以下context.expand('$ {Foo#Response}')。

相关问题