空手道:当使用三元 (?:) 运算符并将值分配给使用 def 关键字声明的变量时,xml 被类型转换为 json

Karate : xml is being type casted to json when ternary(?:) operator is used and value assigned to variable declared with def keyword

我有两个文件,一个是 json,一个是 xml。我想在 isJson 标志的基础上打印 json 或 xml 文件内容。但是我看到当 isJson == false 从下面的程序返回 json 内容时。

Scenario: ternary operator over different type files
* def isJson = false
* json jsonFile = {name : 'user'}
* xml xmlFile = <name>user</name>
* def payload = isJson == true ? jsonFile : xmlFile
* print 'payload --->',payload

控制台输出:

INFO  com.intuit.karate - [print] payload ---> {
 "name": "user"
}

空手道确实尝试 auto-detect,但有时可能无法打印 - 但在幕后它是 XML,您可以正常发送/使用它。我还强烈建议您不要编写此类动态测试。请仔细阅读:

所以你可能不得不忍受这个,或者请贡献代码。请注意,如果您使用 karate.prettyXml(),您可以看到打印为 XML 的变量(如果是)。下一个 1.0 版本可能会改善这一点,但不能保证。