Tim Mottram
Tim Mottram

Reputation: 29

SOAPUI Property Transfer xpath

I am trying to transfer the response from one test case to another in SOAPUI. So that i can search by country and retrieve the currency code to use then search by currency code. However i am not sure i am getting my xpath correct.

SOAP WSDL used: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

This is a practice for a service which is shortly due to be delivered.

Initial test case post

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET">
   <soap:Header/>
   <soap:Body>
      <web:GetCurrencyByCountry>
         <!--Optional:-->
         <web:CountryName>Belgium</web:CountryName>
      </web:GetCurrencyByCountry>
   </soap:Body>
</soap:Envelope>

Response

  <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetCurrencyByCountryResponse xmlns="http://www.webserviceX.NET">
         <GetCurrencyByCountryResult><![CDATA[<NewDataSet>
  <Table>
    <Name>Belgium</Name>
    <CountryCode>be</CountryCode>
    <Currency>Franc</Currency>
    <CurrencyCode>BEF</CurrencyCode>
  </Table>
  <Table>
    <Name>Belgium</Name>
    <CountryCode>be</CountryCode>
    <Currency>Franc</Currency>
    <CurrencyCode>BEF</CurrencyCode>
  </Table>
</NewDataSet>]]></GetCurrencyByCountryResult>
      </GetCurrencyByCountryResponse>
   </soap:Body>
</soap:Envelope>

I am then using the property transfer test step, Firstly setting the drop downs as follows:

Source: Get Currency by Country; Property: Response Path Language: xpath

Then declared the xpath as follows:

declare namespace sam= 'http://www.webserviceX.NET';//GetCurrencyByCountryResult/table[2]/CurrencyCode

Each time i run the test i get a Null response. I have attempted using a wild card however i still cannot pick up the value.

Upvotes: 0

Views: 1451

Answers (2)

Suman g
Suman g

Reputation: 477

You need to store the CDATA Content in to a property and then use that property value in groovy script and access CDATA internal xml, I am referring in the below example <![CDATA[<isle>test</isle>]]> .

eg:
 <test>
<elementa>
<![CDATA[<isle>test</isle>]]>
</elementa>
</test>

In your current scenario Soapui only understand XPATH up to "//GetCurrencyByCountryResult" , after that whatever path have provided goes under CDATA .

SOAPUI working with CDATA is separate mechanism.

Please look into this; it has lot of information on CDATA with SOAPUI and its definitely solve your issue

https://www.soapui.org/functional-testing/working-with-cdata.html

Upvotes: 0

Rao
Rao

Reputation: 21379

Here is the groovy script which does exactly what you are looking for:

Added comments appropriately before each statement for better understanding.

Currently using the fixed xml response. But you may replace it to make it dynamic.

Since your xml has cdata and cdata has xml again. So, this needs to be done in two phases, first time cdata and then xpath to get the actual value that is needed.

import com.eviware.soapui.support.XmlHolder
def xml = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <soap:Body>
      <GetCurrencyByCountryResponse xmlns="http://www.webserviceX.NET">
         <GetCurrencyByCountryResult><![CDATA[<NewDataSet>
  <Table>
    <Name>Belgium</Name>
    <CountryCode>be</CountryCode>
    <Currency>Franc</Currency>
    <CurrencyCode>BEF</CurrencyCode>
  </Table>
  <Table>
    <Name>Belgium</Name>
    <CountryCode>be</CountryCode>
    <Currency>Franc</Currency>
    <CurrencyCode>BEF</CurrencyCode>
  </Table>
</NewDataSet>]]></GetCurrencyByCountryResult>
      </GetCurrencyByCountryResponse>
   </soap:Body>
</soap:Envelope>'''
//If you want this script to handle dynamic response
// remove above xml and uncomment below line and replace your previous step name in
// place of STEP_NAME
//def xml = context.expand( '${STEP_NAME#Response}' )
//Please replace the actual response in place of xml
def holder = new XmlHolder(xml)
//Get the CDATA part
def countryCodeResult = holder.getNodeValue('//*:GetCurrencyByCountryResult')
//log the content of CDATA
log.info countryCodeResult
//Again convert cdata string as xml holder
def cdataHolder = new XmlHolder(countryCodeResult)
//Get the actual xpath
def currencyCode = cdataHolder.getNodeValue("//Table[2]/CurrencyCode")
log.info currencyCode
//now you may store this currency code as suite level propery so that
//you can use it in next test case as ${#TestSuite#CURRENCY_CODE}
context.testCase.testSuite.setPropertyValue('CURRENCY_CODE', currencyCode)

As you see in the above comment, the script is storing currency code into a test suite level property. That allows you to use the currency code in different test case or step using property expansion, say ${#TestSuite#CURRENCY_CODE}

Upvotes: 0

Related Questions