Karate 实现不存在空手道的xpath的if条件

Karate 实现不存在空手道的xpath的if条件,karate,Karate,我正在使用xml响应体,并试图在xpath不存在的情况下保持测试运行。这是我的特征文件 When request read("propertyAvail.xml") And header CWT_DEBUG_LABELS = true And header CWT_DEBUG_RATES_RANK_LABELS = true And header Content-Type = "text/xml" And header CWT_TRAVELER_ID = "A:65887F3" And head

我正在使用xml响应体,并试图在xpath不存在的情况下保持测试运行。这是我的特征文件

When request read("propertyAvail.xml")
And header CWT_DEBUG_LABELS = true
And header CWT_DEBUG_RATES_RANK_LABELS = true
And header Content-Type = "text/xml"
And header CWT_TRAVELER_ID = "A:65887F3"
And header CWT_TRAVELER_ID_TYPE = "portrait"
And method post
Then match response //BookingChannel[2]/RatePlan[1]/TPA_Extensions/InPolicy == "#notnull"
And def inPol = //BookingChannel[2]/RatePlan[1]/TPA_Extensions/InPolicy
And def inPolVal = //BookingChannel[2]/RatePlan[1]/TPA_Extensions/Labels/Label[@Type="CWT_INPOLICY_VALUE"]
And def rateIsInPolicy = Java.type("com.cwt.karate_scripts.CalculationHelper").isInPolicy(inPol, inPolVal)
And match rateIsInPolicy == true
在这里,我想实现逻辑

if (//BookingChannel[2]/RatePlan[1]/TPA_Extensions/InPolicy -> does not exist){
  And def inPolVal = 0.0  
  And def rateIsInPolicy = Java.type("com.cwt.karate_scripts.CalculationHelper").isInPolicy(inPol, inPolVal)
  And match rateIsInPolicy == true

else{ 

  And def inPolVal = //BookingChannel[2]/RatePlan[1]/TPA_Extensions/Labels/Label[@Type="CWT_INPOLICY_VALUE"]
  And def rateIsInPolicy = Java.type("com.cwt.karate_scripts.CalculationHelper").isInPolicy(inPol, inPolVal)
  And match rateIsInPolicy == true
所以我希望保持相同的特性、相同的场景,即使xpath不存在,也要保持测试运行,并在之后执行一些操作。我怎样才能做到?也许空手道中也存在着试抓逻辑?请提供帮助。

如果XPath不存在,
karate.get()
API将优雅地返回null。所以这是可行的:

* def xml1 = <root><one>1</one></root>
* def temp = karate.get('$xml1/root/one')
* def result = temp ? 'yes' : 'no'
* match result == 'yes'

* def xml2 = <root><two>2</two></root>
* def temp = karate.get('$xml2/root/one')
* def result = temp ? 'yes' : 'no'
* match result == 'no'
*def xml1=1
*def temp=karate.get(“$xml1/root/one”)
*def结果=温度?'是:'否'
*匹配结果==“是”
*def xml2=2
*def temp=karate.get(“$xml2/root/one”)
*def结果=温度?'是:'否'
*匹配结果==“否”

现在只要使用上的文档中的技术,您就应该能够做您想做的事情。还有一件事,调用第二个(可重用的)特征文件并没有什么错误,它可以在某些情况下使事情变得简单。

我只留下了前面的一个注释,我们可以认为它是关闭的吗?我完全接受了答案。感谢您的帮助和快速响应!