从请求中提取肥皂标题并将其添加到以下请求中 [英] Extract soap header out of request and add it to following request

查看:92
本文介绍了从请求中提取肥皂标题并将其添加到以下请求中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从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:页眉和GT; 
< 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>全部< / 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>全部< / web:ToCurrency>
< / web:ConversionRate>
< / soapenv:Body>
< / soapenv:Envelope>

希望这是有道理的,并且提供关于如何在xml中获取Header的节点值并插入它的任何建议进入另一个xml的头部值将非常感激

谢谢 解决方案

<根据评论,添加下面的答案。这是与另一个完全不同的解决方案。




  • 这是第一个请求测试步骤的脚本断言
  • 与其他解决方案不同,您不必定义元素名称
  • 脚本自动从第一个请求中读取标题并创建元素名称及其值的映射
  • li>
  • 这还读取下一个测试步骤请求,并将上述步骤映射数据作为标题添加到xml中。
  • 将更改xml更新为下一个测试步骤请求。 / li>


以下是Script Assertion(用于第一个测试步骤):您也可以按照在线评论。

  //如果需要,编辑下一个测试步骤名称的名称
def nextStepName ='SOAP Request2'

//检查当前请求是否为空
断言context.request,'请求为空或空'

def nextStep = context.testCase.testSteps [nextStepName]

log.info下一步需要编辑前的est:$ {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()]}
}


//读取并解析当前请求并获取地图
def currentRequestHeaders = getRequestHeaderMap(getXml (context.request))
log.info当前请求头参数:$ currentRequestHeaders

//读取并解析下一步请求
def nextRequest = getXml(nextStep.testRequest .requestContent)

//删除现有的头文件
nextRequest.Header.replaceBody {}

//用当前请求头更新下一个请求xml
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更新下一个请求:$ nextRequestString

//更新下一个测试步骤的请求内容
nextStep.testRequest .requestContent = nextRequestString


I'm trying to extract the header from a previous request in SoapUI so I can use it in another request. Basically, I want to get the node value for Header in one xml and insert it into the header value of another xml. I have tried using XMLSlurper and XMLParser but not getting exactly what I want. I can extract out the text from the nodes but need the actual whole header value so it can be inserted into other requests as needed.

text = testRunner.testCase.testSteps["ConversionRate"].testRequest.response.getRequestContent()
log.info text
def slurped = new XmlSlurper().parseText(text)
log.info slurped.Header

This results in Value1Value2 using the XML example below but I want to extract out the whole header so it looks like

<soapenv:Header>
<soapenv:MyTag>value1</soapenv:MyTag>
<soapenv:MyTag2>value2</soapenv:MyTag2>
</soapenv:Header>

Sample XML for the purpose of this question is below

<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>

Once I have the value I will need to insert it into the header as an xml sample such as below

<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>

Hope this makes sense and any suggestions on how to get the node value for Header in xml and insert it into the header value of another xml would be very appreciated

Thanks

解决方案

Based on the comments, adding the below as answer. And this is completely different solution from the other one.

  • This is a script assertion for first request test step
  • You do not have to define the element names unlike the other solution
  • The script automatically reads the headers from the first request and creates a map of element name and its value
  • This also reads next test step request and adds the above step map data as headers into the xml
  • Updates the changes xml to next test step request.

Here is the Script Assertion (for the first test step): You may also follow the in-line comments.

//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

这篇关于从请求中提取肥皂标题并将其添加到以下请求中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆