【问题标题】:SOAPUI Property Transfer xpathSOAPUI 属性传输 xpath
【发布时间】:2016-06-02 11:23:18
【问题描述】:

我试图在 SOAPUI 中将响应从一个测试用例转移到另一个测试用例。这样我就可以按国家搜索并检索要使用的货币代码,然后按货币代码搜索。但是我不确定我的 xpath 是否正确。

使用的 SOAP WSDL:http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

这是对即将交付的服务的一种做法。

初始测试用例发布

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

回应

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

然后我正在使用属性转移测试步骤,首先将下拉菜单设置如下:

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

然后声明xpath如下:

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

每次运行测试时,我都会收到 Null 响应。我尝试使用通配符,但仍然无法获取值。

【问题讨论】:

标签: groovy soapui


【解决方案1】:

这是完全符合您要求的 groovy 脚本:

在每个语句之前适当地添加了 cmets 以便更好地理解。

当前使用固定的 xml 响应。但您可以替换它以使其具有动态性。

因为你的 xml 有 cdata 并且 cdata 又有 xml。因此,这需要分两个阶段完成,首先是 cdata,然后是 xpath,以获取所需的实际值。

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)

正如您在上面的评论中看到的,脚本将货币代码存储到测试套件级别的属性中。这允许您在不同的测试用例或使用属性扩展的步骤中使用货币代码,例如${#TestSuite#CURRENCY_CODE}

【讨论】:

  • @TIM 如果它解决了你的问题,它的好做法和鼓励。更重要的是,您会因接受答案而获得声誉积分
【解决方案2】:

您需要将 CDATA 内容存储到一个属性中,然后在 groovy 脚本中使用该属性值并访问 CDATA 内部 xml,我在下面的示例中指的是 &lt;![CDATA[&lt;isle&gt;test&lt;/isle&gt;]]&gt;

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

在您当前的场景中,Soapui 只能理解 XPATH 到 "//GetCurrencyByCountryResult" ,之后提供的任何路径都在 CDATA 下。

使用 CDATA 的 SOAPUI 是单独的机制。

请调查一下;它有很多关于带有 SOAPUI 的 CDATA 的信息,它绝对可以解决您的问题

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

【讨论】:

  • 感谢 Suman,但参考是针对 SOAPUI pro 而不是 SOAPUI 社区版
  • 在这个场景中没有太大的区别,soapui pro 将提供访问元素的所有好处,我的意思是 GETDATA--> 除了在 SOAPUI 社区版和 SOAPUI Pro 中使用 CDATA 之外我也有同样的感觉。无论如何谢谢
  • 谢谢,这很有帮助,但我的主要目的是确定我的 xpath 是否正确。因为我无法让它工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多