我正在尝试从SoapUI中的先前请求中提取标头,以便我可以在另一个请求中使用它。基本上,我想在一个xml中获取Header的节点值,并将其插入另一个xml的头值。我尝试过使用XMLSlurper和XMLParser,但没有得到我想要的。我可以从节点中提取出文本但需要实际的整个标头值,以便可以根据需要将其插入到其他请求中。
text = testRunner.testCase.testSteps["ConversionRate"].testRequest.response.getRequestContent()
log.info text
def slurped = new XmlSlurper().parseText(text)
log.info slurped.Header
这会导致Value1Value2使用下面的XML示例,但我想提取整个标题,使其看起来像
<soapenv:Header>
<soapenv:MyTag>value1</soapenv:MyTag>
<soapenv:MyTag2>value2</soapenv:MyTag2>
</soapenv:Header>
用于此问题的示例XML如下所示
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.webserviceX.NET/">
<soapenv:Header>
<soapenv:MyTag>value1</soapenv:MyTag>
<soapenv:MyTag2>value2</soapenv:MyTag2>
</soapenv:Header>
<soapenv:Body>
<web:ConversionRate>
<web:FromCurrency>AFA</web:FromCurrency>
<web:ToCurrency>ALL</web:ToCurrency>
</web:ConversionRate>
</soapenv:Body>
</soapenv:Envelope>
一旦有了值,我就需要将它作为xml样本插入标题中,如下面的
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.webserviceX.NET/">
<soapenv:Header/>
<soapenv:Body>
<web:ConversionRate>
<web:FromCurrency>AFA</web:FromCurrency>
<web:ToCurrency>ALL</web:ToCurrency>
</web:ConversionRate>
</soapenv:Body>
</soapenv:Envelope>
希望这是有道理的,如何在xml中获取Header的节点值并将其插入另一个xml的头值中的任何建议都将非常感激
由于
答案 0 :(得分:2)
根据评论,添加以下内容作为答案。这与另一个完全不同。
以下是脚本断言(针对第一个测试步骤):您也可以按照内嵌评论进行操作。
//Edit the name of the next test step name if required
def nextStepName = 'SOAP Request2'
//Check if the current request is empty
assert context.request, 'Request is empty or null'
def nextStep = context.testCase.testSteps[nextStepName]
log.info "Next step request before edit: ${nextStep.testRequest.requestContent}"
def getXml = { req -> new XmlSlurper().parseText(req) }
def getRequestHeaderMap = { x ->
def header = x.'**'.find {it.name() == 'Header'}
header.children()*.name().collectEntries {[(it): header."$it".text()]}
}
//Read and Parse current request and get headers as map
def currentRequestHeaders = getRequestHeaderMap(getXml(context.request))
log.info "Current request header parameters : $currentRequestHeaders"
//Read and Parse next step request
def nextRequest = getXml(nextStep.testRequest.requestContent)
//Remove existing headers
nextRequest.Header.replaceBody {}
//Update next request xml with current request headers
currentRequestHeaders.collect { k, v -> nextRequest.Header.appendNode { "ns11:$k"('xmlns:ns11': 'http://schemas.xmlsoap.org/soap/envelope/', v) } }
def nextRequestString = groovy.xml.XmlUtil.serialize(nextRequest)
log.info "Updating next request with : $nextRequestString"
//Update next test step's request content
nextStep.testRequest.requestContent = nextRequestString
答案 1 :(得分:1)
您没有使用额外的groovy脚本来实现相同的目标。而是使用以下代码为第一个SOAP请求测试步骤添加Script Assertion
。
脚本:按照内嵌评论
//Define your element names to extract the data
def elements = ['MyTag', 'MyTag2']
//Don't modify anything beyond this point
//Check if the request is empty
assert context.request, 'Request is empty or null'
//Parse response
def xml = new XmlSlurper().parseText(context.request)
//Closure to find the element data
def getData = { element -> xml.'**'.find {it.name() == element}?.text() }
//Find the tag value and set the same as
elements.each { context.testCase.setPropertyValue(it, getData(it)) }
在下一个要添加标题的请求中进行以下更改。请注意标题和属性扩展。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.webserviceX.NET/">
<soapenv:Header>
<soapenv:MyTag>${#TestCase#MyTag}</soapenv:MyTag>
<soapenv:MyTag2>${#TestCase#MyTag2}</soapenv:MyTag2>
</soapenv:Header>
<soapenv:Body>
<web:ConversionRate>
<web:FromCurrency>AFA</web:FromCurrency>
<web:ToCurrency>ALL</web:ToCurrency>
</web:ConversionRate>
</soapenv:Body>
</soapenv:Envelope>