Downloading SOAP response as xml file

北城余情 提交于 2020-01-06 06:45:38

问题


I have a client who fetches content from a SOAP service. Because it uses a lot of resources and I need to test a lot of things I don't want to mess with their server capabilities.

Thats why I need a way to download the raw XML response to an .xml file. What would be the easiest way to accomplish this?

I have already tried to make a dump to a file with SOAPUI but this doesn't allow to automate the soap calls and download all the files in a sequence.


回答1:


Similar to what albciff suggested, though I only wanted specific requests to be logged:

// 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");

This can similarly be done with response as well. Just change the following context.expand('${Foo#Response}').




回答2:


If you want to execute a whole Test Case of SOAP Test request with SOAPUI saving all the responses, you can add a groovy script step as last step in your testCase with the following code:

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

With this code all the responses for your's testSteps are save to disc (the code comments explains the behavior)

In addition if you want to run the testCase many times avoiding overwrite generated files you can use for example a random generated UUID to create the file name, try with:

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

instead of

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

Hope this helps,



来源:https://stackoverflow.com/questions/22010177/downloading-soap-response-as-xml-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!