Using Groovy Script in sopaUI - Copy the content of a XML Holder to Another (Trying to CLONE the SOAP request Test Step)

橙三吉。 提交于 2020-01-06 23:48:10

问题


I am a newbie to groovy scripting and seeking your help to find a solution to the issue I am facing at the moment.

My requirement is that, on each iteration based on the test data input, I will have to remove a particular node or certain nodes from the SoapRequest. In order to achieve that I created two identical SOAP Request -Original and Modified.

Using groovy script I am trying to restore the content of the modified soap request with the content of the original soap request after each iteration. (Iteration 1 - Node to delete is and in second iteration node to delete is keeping - This is the reason for restoring the request with the original content). In other words want to clone the soap request so that the delete node operations can be performed on the cloned request keeping the original request unchanged.

The below are my Teststeps under my TestSuite.

Datasource Original (SoapRequest) Groovy Script. Modified (SoapRequest)

SoapRequest (Original)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:idm="http://vedaxml.com/vxml2/idmatrix-v2-0.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <idm:request>
         <idm:dataset-searches>
            <idm:profile-name></idm:profile-name>
         </idm:dataset-searches>
         <idm:individual-name>
            <idm:family-name>ABC</idm:family-name>
            <idm:first-given-name>DEF</idm:first-given-name>
         </idm:individual-name>
         <idm:date-of-birth>1985-12-12</idm:date-of-birth>
      </idm:request>
   </soapenv:Body>
</soapenv:Envelope>

My Groovy Script is as below

def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
def ReqHolder2 = grUtils.getXmlHolder("Modified#Request")

ReqHolder2.removeDomNodes("//idm:request")
ReqHolder2.updateProperty()

ReqHolder2 ["//soapenv:Body"] = context.expand( '${Original#Request#//idm:request}' )
ReqHolder2.updateProperty()

When I execute the above groovy script, the Modified request is updated with the content from the Original request but with CDATA and reference to the schema.

SoapRequest (Modified)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:idm="http://vedaxml.com/vxml2/idmatrix-v2-0.xsd">
   <soapenv:Header/>
   <soapenv:Body>***<![CDATA[<idm:request xmlns:idm="http://vedaxml.com/vxml2/idmatrix-v2-0.xsd">***
         <idm:dataset-searches>
            <idm:profile-name/>
         </idm:dataset-searches>
         <idm:individual-name>
            <idm:family-name>ABC</idm:family-name>
            <idm:first-given-name>DEF</idm:first-given-name>
         </idm:individual-name>
         <idm:date-of-birth>1985-12-12</idm:date-of-birth>
      </idm:request>**]]>**</soapenv:Body>
</soapenv:Envelope>

I would greatly appreciate if someone could help me with this. Also, I would be glad to know/learn other alternative ways in groovy to implement this requirement.

Thank you.


回答1:


Consider reading on Groovy XmlSlurper and XmlParser. They are very simple to implement and you could use them for your xml manipulation. However, for your particular requirement, using XmlParser would make more sense.

Here is a different question and my answer, just to help you get an idea of how XMLParser works

And since your question is more about deleting nodes and less about accessing/validating them. Here is an other question on SO about deletion of nodes using XMlParser.



来源:https://stackoverflow.com/questions/11625796/using-groovy-script-in-sopaui-copy-the-content-of-a-xml-holder-to-another-try

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