0

I am using SoapUI to test webservices. The following string (in xml format) is my request:

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

I am using the following code in a groovy script to extract value of Asset_Number for each AC (The above xml string is stored in variable strRequest):

def x = new XmlSlurper().parseText("$strRequest")

x.AC.each { AC ->
assetNum = AC."Asset_Number"
<<do something with the assetNum>>
}

However, I wish to parameterize the above code to pick up Asset_Number for various types of assets (e.g. AC, Peripheral etc). The request xml for each asset is in the same format as above. If I replace 'AC' with variable name 'requestName' in above code:

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

it shows the following error:

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

I have tried a solution mentioned in another post Using a String as code with Groovy XML Parser, but it doesn't serve my purpose.

Any other ideas?

Community
  • 1
  • 1
Daisy
  • 1
  • 2

2 Answers2

1

You can use

x."$requestName".each
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I tried using "$requestName" as follows: `x."$requestName".each { "$requestName" -> assetNum = "$requestName"."Asset_Number" <> }` but it shows me another error as follows: `org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script212.groovy: 31: expecting '}', found '->' @ line 31, column 18. "$requestName" -> ^ org.codehaus.groovy.syntax.SyntaxException: expecting '}', found '->' @ line 31, column 18.` – Daisy Aug 19 '14 at 12:12
0

Why XmlSlurper? In SoapUI you can use XmlHolder

import com.eviware.soapui.support.XmlHolder

def responseHolder = new XmlHolder(messageExchange.getResponseContentAsXml())

def resultFromResponse = responseHolder["here_is_your_xpath"]
assert someExpectedResult == resultFromResponse

And if you need to iterate via multiple xml nodes

resultFromResponse.each 
{
    assert result == resultFromResponse     
}
olyv
  • 3,699
  • 5
  • 37
  • 67