我正在使用SoapUI来测试webservices。以下字符串(xml格式)是我的请求:
<Request>
<AC>
<AssetID>1</AssetID>
<Asset_Name>ABC</Asset_Name>
<Asset_Number>1</Asset_Number>
</AC>
<AC>
<AssetID>2</AssetID>
<Asset_Name>XYZ</Asset_Name>
<Asset_Number>2</Asset_Number>
</Ac>
</Request>
我在groovy脚本中使用以下代码来为每个AC提取Asset_Number的值(上面的xml字符串存储在变量strRequest中):
def x = new XmlSlurper().parseText("$strRequest")
x.AC.each { AC ->
assetNum = AC."Asset_Number"
<<do something with the assetNum>>
}
但是,我希望参数化上面的代码来为各种类型的资产(例如AC,外围设备等)获取Asset_Number。每个资产的请求xml与上面的格式相同。如果我在上面的代码中用变量名'requestName'替换'AC':
//strRequest = xml request
def requestName //I will pick up value for this from a test case property
def x = new XmlSlurper().parseText("$strRequest")
x.(requestName.toString()).each { requestName ->
assetNum = requestName."Asset_Number"
<<do something with the assetNum>>
}
它显示以下错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script166.groovy: 35: The current scope already contains a variable of the name requestName @ line 35, column 2. { ^ org.codehaus.groovy.syntax.SyntaxException: The current scope already contains a variable of the name requestName
我尝试了另一篇帖子Using a String as code with Groovy XML Parser中提到的解决方案,但它不符合我的目的。
还有其他想法吗?
答案 0 :(得分:1)
您可以使用
x."$requestName".each
答案 1 :(得分:0)
为什么选择XmlSlurper?在SoapUI中,您可以使用XmlHolder
import com.eviware.soapui.support.XmlHolder
def responseHolder = new XmlHolder(messageExchange.getResponseContentAsXml())
def resultFromResponse = responseHolder["here_is_your_xpath"]
assert someExpectedResult == resultFromResponse
如果你需要通过多个xml节点进行迭代
resultFromResponse.each
{
assert result == resultFromResponse
}