karate - 为不存在空手道的 xpath 实现 if 条件
问题描述
我正在使用 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 不存在并在此之后执行一些操作。我该怎么做?也许空手道中也存在 try-catch 逻辑?请帮忙。
解决方案
karate.get()
如果 XPath 不存在,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'
现在只需使用条件逻辑文档中的技术,您应该能够做您想做的事。还有一件事,调用第二个(可重用)功能文件并没有错,它可以在某些情况下使事情变得更简单。
推荐阅读
- chart.js - 在Chartjs中为指针填充颜色
- reactjs - Test failed of a Component in react using typescript
- sql - Trying to understand why GROUP BY and HAVING yields different output when compared to WHERE
- html - 添加容器 div 会移除 flexbox
- amazon-s3 - Amazon S3 PDF files have wrong content type after Winscp upload
- python - Python to PHP? For twitter scraper (Scweet)
- javascript - 使用 Map 解析列表项的正确方法是什么?
- python - discord bot - userinfo command "IndentationError: unexpected indent"
- intellij-idea - IntelliJ 中的奇怪 V
- python - Python Selenium 文本从 for 循环中提取列表输出