【问题标题】:groovy xml parsing and modificationgroovy xml解析和修改
【发布时间】:2015-09-02 09:37:37
【问题描述】:

我正在尝试使用 groovy 构建测试自动化脚本。我的输入模板就像 myXML 变量

我正在从 excel 文件中读取输入数据并替换值并通过 SOAPUI 触发请求。

我的代码是这样的

def myXML ='''<soapenv:Body    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="xyz.org/common" xmlns:fin="xyz.org/finance">
<fin:myBalance>
    <com:loginName>test</com:loginName>
     <fin:params>
        <fin:name value="username"/>
        <fin:Id value="12345"/>
        <fin:nickname value="usr1"/>
    </fin:params>
</fin:myBalance>
</soapenv:Body> '''

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def reqHolder = groovyUtils.getXmlHolder("InputReq#Request")

reqHolder.namespaces['soapenv']= 'http://schemas.xmlsoap.org/soap/envelope/';
reqHolder.namespaces['com']= 'xyz.org/common';
reqHolder.namespaces['fin']= 'xyz.org/common';

reqHolder.setNodeValue("//fin:myBalance[1]/${newTag.getContents()}[1]",
                        "${newValue.getContents()}");

如果我想更改节点值,这非常有效。但我无法找到使用 reqHolder 更改属性的方法。说我想改变 fin:Id value="12345" 到 "6789"

有办法改变吗?

【问题讨论】:

  • 您做的最简单的事情就是使用属性。即,拥有&lt;fin:Id value="${#TestCase#Id}"/&gt; 并在测试用例 ID 处使用您想要的任何值定义自定义属性。您可以随时使用 groovy 脚本手动或自动更新 context.testCase.setPropertyValue('Id', newValue)

标签: xml parsing groovy namespaces identify


【解决方案1】:

您可以尝试使用 Groovy 内置的 XmlParser,而不是使用 SoapUI 的 API。有了它,您可以更改属性。例如:

def xml = '<root><one a1="uno!"/><two>Some text!</two></root>'
def rootNode = new XmlParser().parseText(xml)
assert rootNode.one[0].@a1 == 'uno!'

来源:http://docs.groovy-lang.org/latest/html/api/groovy/util/XmlParser.html

你需要做的是获取xm,例如:

 def response = context.testCase.testSteps['Properties'].properties['response'].value

更多 XML 处理技巧在这里:http://www.robert-nemet.com/2011/11/groovy-xml-parsing-in-soapui.html

获取 XML 的另一种方式: Groovy script to get the request xml

您可以在此处找到有关 Groovy 的 XML 处理的更多信息: http://groovy-lang.org/processing-xml.html

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2020-12-20
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多