如何在空手道中将JS EXPR与XML结合使用

时间:2018-12-27 11:57:24

标签: karate

我必须修改xml表达式并将其与json匹配。以下是我编写的代码。我需要验证ind是对还是错。为此,我不能使用'#boolean',因为<ns4:ind>true</ns4:ind>表现为字符串。

XML文件:

<ns9:parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:parent">
        <ns4:ind>true</ns4:ind>
        <ns4:description>Some text</ns4:description>
</ns9:parent>

代码:

* def Test = call read ('classpath:Path/Test.xml)
* def Check = $Test/parent
* set Downstream.parent
| path           | value                              |
| ind            | '#?_ == "true" || _== "false"'     |
| description    | $Check/parent/description          |


* def Match = 
"""
{
  "parent": {
    "ind": "true",
    "description": "Some text"
  }
}
"""
* match Match == Downstream

也尝试过'#boolean',不起作用。

    | ind  | '#boolean' |

我遇到错误;

com.intuit.karate.exception.KarateException: Test.feature:20 - javascript evaluation failed: '#?_   == "true", <eval>:1:14 Missing close quote '#?_ == "true"

1 个答案:

答案 0 :(得分:1)

这应该有效,

* def Test = call read ('classpath:Path/Test.xml')
* def Check = $Test/parent
* xml Downstream = "<parent></parent>"
* def desc = $Check/parent/description   
* set Downstream/parent/ind = '#?_ == "true" || _== "false"'
* set Downstream/parent/description = desc
* match $Test/parent/ind == $Downstream/parent/ind
* match $Test/parent/description == $Downstream/parent/description
  

注意:     为XML设置的值似乎不支持表,因此请使用内联集。