如何动态地使用groovy脚本将childNode(特别是包括属性)添加到XML请求中

时间:2019-03-29 08:57:20

标签: xml groovy soapui ready-api

我正在尝试使用readyAPI中的常规脚本在测试用例中的每个SOAP请求测试步骤中,将一个childNode添加到SOAP请求中。到目前为止,我能够添加一个包括名称空间前缀的childNode。但是我几乎无法添加带有属性的单行子节点。

对于适用于我的属性,我找不到太多有用的信息。这是我的常规脚本,我从中得到的东西,还有我想要得到的东西。

这是我使用的完整脚本,只是不包含nameSpace URL

import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep 
import groovy.xml.*

def myTestCase = testRunner. testCase
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def soapRequestList = []
assert soapRequestList.size() == 0

// Holding all SOAP request step names in an arrayList

log.info "Test Step Count : " + context.testCase.testStepCount(0..context.testCase.testStepCount-1).each{
    def step = context.testCase.testStepList[it]
    if ( step instanceof WsdlTestRequestStep) {
        soapRequestList.add(step.name)
    }
}

log.info "SOAP Request Count: " + soapRequestList.size()

// Nessesery parameter definitions

def holderWebServiceRequest
def holderDynamicRequest 
def holderDynamic

// Dynamic Request Creation

def nodeName = "transactionId"

for(int i=0; i<soapRequestList.size() ; i++){

    // Defining web service request variable for each step

    holderWebServiceRequest = soapRequestList[i] + "#Request#"
    holderDynamicRequest = soapRequestList[i] + "#Request"
    holderDynamic = groovyUtils.getXmlHolder(""+holderDynamicRequest+"")

    //Deleting all child nodes with defined nodeName parameter already exists in the requestHeader

    int tidCount=context.expand('${'+holderWebServiceRequest+'count(//*:transactionId)}').toInteger()

    if(tidCount != 0 ){

        for (index = 1; index <= tidCount; index++) {

            deleteChildNode("//*:requestHeader", "//*:requestHeader//*:transactionId", holderDynamic)

            }
    }

    //Adding new child node with a testCase-property, that is unique for each request

    i=i+1
    addChildNode("//*:requestHeader",'''<transactionId/>'''.stripMargin(), "//transactionId" , i , nodeName ,  holderDynamic)
    i=i-1

}

// Function Definitions

void deleteChildNode (def parentNode, def childNode, def holderDynamic) {

    def parentnodeDelete  = holderDynamic.getDomNode( parentNode)
    def childnodeDelete = holderDynamic.getDomNode(childNode) 
    parentnodeDelete.removeChild(childnodeDelete)
    holderDynamic.updateProperty()

}

void addChildNode (def parentNode, def childNode, def object, def ctidPrefix, def nodeName, def holderDynamic){

    def parentnodeAdd  = holderDynamic.getDomNode( parentNode)
    def nodeHolderchildNodeElement  = new com.eviware.soapui.support.XmlHolder( childNode )
    def childNodeElementItem    = nodeHolderchildNodeElement.getDomNode( object )
    def importChildNode  = parentnodeAdd.getOwnerDocument().importNode( childNodeElementItem, true )
    def ns = "URL FOR THE NAMESAPACE PREFIX DEFINED IN THE XML FILES"
    def child = parentnodeAdd.appendChild( parentnodeAdd.getOwnerDocument().createElementNS(ns, nodeName ))
    child.appendChild(parentnodeAdd.getOwnerDocument().createTextNode('''${#TestCase#transactionID_'''+ ctidPrefix +'''}'''))
    holderDynamic.updateProperty(true)

}

例如,如果这是测试用例中第六个SOAP请求步骤的XML请求的一部分:它具有多个transactionId节点,并且所有节点的属性索引均错误:

<webService xmlns:ns2="URL FOR THE NAMESAPACE PREFIX DEFINED IN THE XML FILES">
    <requestHeader>
        <ns2:application>${#Project#application}</ns2:application>
        <ns2:languageCode>${#Project#languageCode}</ns2:languageCode>
        <ns2:transactionId>${#TestCase#transactionID_2}</ns2:transactionId>
        <ns2:channel>${#Project#channel}</ns2:channel>
        <ns2:extraParameters key="USER_IP" value="${#Project#myIP}"/>
        <ns2:transactionId>${#TestCase#transactionID_3}</ns2:transactionId>
    </requestHeader>

运行脚本时,它将删除所有transactionId子节点,并在其中写入一个带有nessessery属性的新节点。之后,它看起来像这样:

<webService xmlns:ns2="URL FOR THE NAMESAPACE PREFIX DEFINED IN THE XML FILES">
    <requestHeader>
        <ns2:application>${#Project#application}</ns2:application>
        <ns2:languageCode>${#Project#languageCode}</ns2:languageCode>
        <ns2:channel>${#Project#channel}</ns2:channel>
        <ns2:extraParameters key="USER_IP" value="${#Project#myIP}"/>
        <ns2:transactionId>${#TestCase#transactionID_6}</ns2:transactionId>
    </requestHeader>

到目前为止,还不错。

我的问题是:我将如何替换包含选择的“键”和“值”属性的Extraparameters子节点,就像我对transactionId节点所做的那样?我期望extraParameters节点完全与上面的示例类似。

0 个答案:

没有答案