Forum Discussion

Ostr's avatar
Ostr
New Contributor
16 years ago

SoapUI and XMLs

Hi All,

I've created TestCase with request and groovy script reads file and inserts values from file into request. But I have a trouble :

I used "XmlHolder" to working with request. And I found a way how I can remove nodes or edit them in request.
But I've found a big trouble for me :

I've XML with node :

     
    ?


I don't know how many products will be in request.
So, I've tried to search for a function that can add child node () to parent node (), but I didn't found answer.

Please, help me with this easy task (I feel that answer is so simple).

Thanks a lot
  • Ostr's avatar
    Ostr
    New Contributor
    I've read page about groovy script, but as I can understood "Modification of a Request xml" allows me to change only values of nodes, and XmlHolder allows only remove nodes and modify values of nodes.

    If you know how to add nodes to a xml, please, provide part of GroovyScript code.

    Thanks,
    Yuri
  • svensson's avatar
    svensson
    New Contributor
    I have the same problem as Ostr.

    Is it possible at all adding nodes to a xml request?
    Some sample GroovyScript code would be indeed very helpful. thanks

    Relating to soapUI pro support:
    Unfortunately, our company is not yet a pro member. But we are going to buy a licence as soon our customer agrees.
    My problem: I have to write the test cases with the trial version these days. thanks for any support.
  • svensson's avatar
    svensson
    New Contributor
    here's the solution: extract what you need...

    /*
    [Script Name  ]  DLC Parsing
    [Purpose      ]  Parses service codes from an excel test data sheet and insert relating nodes into XML SOAP-request.
    [Created      ]  2009-09-22
    [Last Modified]  -

    [Author      ]  Sven Zaugg
    [Company      ]  Zühlke Engineering AG
    */


    def stepName = "GenerateLabel";

    // read current SOAP request XML file
    def request = testRunner.testCase.getTestStepByName(stepName);
    def property = request.getProperty("request");
    def rootNode = new groovy.util.XmlParser(false,false).parseText(property.value);

    // delete previous service codes
    def tmpAttributeList = [];
    def attributesNodes = rootNode["soapenv:Body"]["typ:$stepName"]["typ:Envelope"]["typ:Data"]["typ:Provider"]["typ:Sending"]["typ:Item"]["typ:Attributes"][0].children();
    for (attribute in attributesNodes) {
    if (attribute.name() != "typ:ATT_PRZL") {
    // [c1]: save temporary attribute nodes for sticking to sequence restriction defined in relating XSD schema
    tmpAttributeList.add(attribute);
    log.info attribute;
    }
    }

    // see comment [c1]
    attributesNodes.clear();

    // read service codes values from excel data sheet
    def dlcField = context.expand( '${ExcelSheet#ATT_PRZL}' )
    def dlcCodes = dlcField.split(',')

    // update SOAP request XML file with service codes
    def attributesNode = rootNode["soapenv:Body"]["typ:$stepName"]["typ:Envelope"]["typ:Data"]["typ:Provider"]["typ:Sending"]["typ:Item"]["typ:Attributes"][0];
    for (code in dlcCodes) {
    attributesNode.appendNode("typ:ATT_PRZL", code);
    }

    // back up old attributes data, see comment [c1]
    for (attribute in tmpAttributeList) {
    attributesNode.append(attribute);
    }

    // save changes in SOAP request XML file before start sending it
    def writer = new java.io.StringWriter();
    def printer = new groovy.util.XmlNodePrinter( new PrintWriter( writer ));
    printer.print( rootNode );
    def xmlString = com.eviware.soapui.support.xml.XmlUtils.prettyPrintXml(writer.toString())
    property.setValue( xmlString )