在sopaUI中使用Groovy脚本 - 将XML Holder的内容复制到另一个(尝试克隆SOAP请求测试步骤) [英] Using Groovy Script in sopaUI - Copy the content of a XML Holder to Another (Trying to CLONE the SOAP request Test Step)

查看:308
本文介绍了在sopaUI中使用Groovy脚本 - 将XML Holder的内容复制到另一个(尝试克隆SOAP请求测试步骤)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的要求是,每个人都需要基于测试数据输入的迭代,我将不得不从SoapRequest中删除特定节点或某些节点。为了实现这一点,我创建了两个相同的SOAP请求-Original和Modified。

使用groovy脚本我试图在每次迭代之后用原始soap请求的内容恢复修改后的soap请求的内容。 (迭代1 - 要删除的节点是在第二个要删除的迭代节点中保存的 - 这是用原始内容恢复请求的原因)。换句话说,希望克隆肥皂请求,以便可以在克隆的请求上执行删除节点操作,以保持原始请求不变。



以下是我的Teststeps TestSuite。

数据源
原始(SoapRequest)
Groovy脚本。
修改(SoapRequest)

SoapRequest(原始)

 < 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:个人名称>
< idm:家庭名称> ABC< / idm:家庭名称>
< idm:first-given-name> DEF< / idm:first-given-name>
< / idm:个人名称>
< idm:出生日期> 1985-12-12< / idm:出生日期>
< / idm:request>
< / soapenv:Body>
< / soapenv:Envelope>

我的Groovy脚本如下

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

当我执行上面的groovy脚本时,修改后的请求会使用原始请求中的内容进行更新,但是会使用CDATA并引用模式。
$ b

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:profile-name />
< / idm:dataset-searches>
< idm:个人名称>
< idm:家庭名称> ABC< / idm:家庭名称>
< idm:first-given-name> DEF< / idm:first-given-name>
< / idm:个人名称>
< idm:出生日期> 1985-12-12< / idm:出生日期>
< / idm:request> **]]> **< / soapenv:Body>
< / soapenv:Envelope>

如果有人能帮助我,我将不胜感激。



谢谢。

解决方案

考虑阅读Groovy XmlSlurper XmlParser 。他们很容易实现,你可以使用它们进行xml操作。但是,对于您的特定要求,使用XmlParser会更有意义。

这是一个不同的问题,,只是为了帮助您了解XMLParser的工作原理



由于您的问题更多的是关于删除节点和更少的访问/验证它们。以下是关于使用XMlParser删除节点的其他问题

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.

解决方案

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.

这篇关于在sopaUI中使用Groovy脚本 - 将XML Holder的内容复制到另一个(尝试克隆SOAP请求测试步骤)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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