问题
i got a Problem.
So i got an WSDL like this:
<node1>
<subnode1>data</subnode1>
<subnode2>data</subnode2>
<subnode3>data</subnode3>
<subnode4>data</subnode4>
<!--Zero or more repetitions:-->
<subnode5>
<subsubnode1>data</subsubnode1>
<subsubnode2>data</subsubnode2>
<subsubnode3>data</subsubnode3>
</subenode5>
</node1>
for Testing via SoapUI, the Problem is now, that the subnode5 can have one ore more reptions, it depends from the Database. Now my questions - how can isolved this to make the repetitions dynamic.
so i try t append the subnode5 via a groovy script like this:
import com.eviware.soapui.support.XmlHolder;
import com.eviware.soapui.support.GroovyUtils;
def groovyUtil = new GroovyUtils( context )
def holder = groovyUtil.getXmlHolder( "name#Request" )
def parentnode = holder.getDomNode( "//node1" )
def text = '''
<subnode5>
<subsubnode1>data</subsubnode1>
<subsubnode2>data</subsubnode2>
<subsubnode3>data</subsubnode3>
</subnode5>
'''.stripMargin()
def nodetext = groovyUtil.getXMLHolder( text )
def nodeItem = nodetext.getDomNode ( "//subnode5")
parentnode.appendChild(nodeItem, true)
holder.updateProperty()
but i get an errormessage:
groovy.lang.MissingMethodException: No signature of method: org.apache.xmlbeans.impl.store.Xobj$ElementXobj.appendChild() is applicable for argument types: (org.apache.xmlbeans.impl.store.Xobj$ElementXobj, java.lang.Boolean) values: [?xml version="1.0" encoding="UTF-8"?> , ...] Possible solutions: appendChild(org.w3c.dom.Node) error at line: 29
what i will is added a new child to the request
<node1>
<subnode1>data</subnode1>
<subnode2>data</subnode2>
<subnode3>data</subnode3>
<subnode4>data</subnode4>
<subnode5>
<subsubnode1>data</subsubnode1>
<subsubnode2>data</subsubnode2>
<subsubnode3>data</subsubnode3>
</subenode5>
--first repition--
<subnode5>
<subsubnode1>data</subsubnode1>
<subsubnode2>data</subsubnode2>
<subsubnode3>data</subsubnode3>
</subenode5>
--second repition--
<subnode5>
<subsubnode1>data</subsubnode1>
<subsubnode2>data</subsubnode2>
<subsubnode3>data</subsubnode3>
</subenode5>
.... and so on
</node1>
回答1:
Here is a roadmap - you will need to adjust it to suit your particular needs!
import com.eviware.soapui.support.GroovyUtils
// create groovyUtils and XmlHolder for request
def grUtils = new GroovyUtils(context)
def requestHolder = grUtils.getXmlHolder("name#Request")
// find the Node that I am interested in
def requestNode = requestHolder.getDomNode("//*:node1")
// the Document object is used to create new nodes
def requestDoc = requestNode.getOwnerDocument()
// create the whole structure 3 times
3.times {
// create a new Element in the Document
def subelement5 = requestDoc.createElement("subnode5")
def subnode5 = requestNode.insertBefore(subelement5, requestNode.getFirstChild())
// create the sub-sub nodes
1..3.each {
def subsubelement = requestDoc.createElement("subsubnode${it}")
subnode5.insertBefore(subsubelement, subnode5.getFirstChild())
// add in the data text
subsubelement.appendChild(requestDoc.createTextNode("data"))
}
}
// write the Document out to the request
requestHolder.updateProperty(true)
Here is some additional reading, if interested.
来源:https://stackoverflow.com/questions/29148600/soap-ui-ad-an-node-to-request-groovy