user1799346
user1799346

Reputation: 21

groovy xml parsing and modification

I am trying to build a test automation script using groovy. My input template is like myXML variable

I am reading input data from excel file and replacing the values and firing the request through SOAPUI.

my code is like this

def myXML ='''<soapenv:Body    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="xyz.org/common" xmlns:fin="xyz.org/finance">
<fin:myBalance>
    <com:loginName>test</com:loginName>
     <fin:params>
        <fin:name value="username"/>
        <fin:Id value="12345"/>
        <fin:nickname value="usr1"/>
    </fin:params>
</fin:myBalance>
</soapenv:Body> '''

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def reqHolder = groovyUtils.getXmlHolder("InputReq#Request")

reqHolder.namespaces['soapenv']= 'http://schemas.xmlsoap.org/soap/envelope/';
reqHolder.namespaces['com']= 'xyz.org/common';
reqHolder.namespaces['fin']= 'xyz.org/common';

reqHolder.setNodeValue("//fin:myBalance[1]/${newTag.getContents()}[1]",
                        "${newValue.getContents()}");

If I want to change node value this works perfectly. But I am not able to find a way to change an attribute using reqHolder. Say I want to change fin:Id value="12345" to "6789"

Is there a way to change ?

Upvotes: 2

Views: 433

Answers (1)

Aston
Aston

Reputation: 3712

Instead of using SoapUI's API, you can try Groovy's built in XmlParser. With that you can change attributes. For example:

def xml = '<root><one a1="uno!"/><two>Some text!</two></root>'
def rootNode = new XmlParser().parseText(xml)
assert rootNode.one[0].@a1 == 'uno!'

Source: http://docs.groovy-lang.org/latest/html/api/groovy/util/XmlParser.html

What you need to do is get the xm, for example:

 def response = context.testCase.testSteps['Properties'].properties['response'].value

More XML processing tips here: http://www.robert-nemet.com/2011/11/groovy-xml-parsing-in-soapui.html

Another way of getting the XML: Groovy script to get the request xml

You can find more about Groovy's XML processing here: http://groovy-lang.org/processing-xml.html

Upvotes: 1

Related Questions