空手道 API 测试 - 使用 xml 而不是 json

Karate API Tests - Using xml instead of json

我可以使用 json 文件在功能文件中解析我的凭据。 例如:

 * def credentials = read('classpath:credentials.json')
 * header Authorization = call read('classpath:basic-auth.js') { username: '#(credentials.user)', password: '#(credentials.pwd)' }

这是凭证 json 文件:

{
  user: 'abc',
  pwd: 'def'
}

但是,当我尝试使用 XML 文件时,我无法通过以下方式解析它:

凭据xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>
 <credentials>  
   <user>abc</user>
   <pwd>def</pwd>
 </credentials>

我将功能文件更改为:

 * def credentials = read('classpath:credentials.xml')
 * header Authorization = call read('classpath:basic-auth.js') { username: '#(<credentials><user></credentials>)', password: '#(<credentials><pwd></credentials>)' }

我是否需要对解析 xml 的方式进行任何更改?任何建议,将不胜感激。提前致谢!!

嵌入表达式必须使用'dot notation'。好消息是这可以在 XML 上运行,所以试试这个:

* def creds = read('classpath:credentials.xml')
* header Authorization = call read('classpath:basic-auth.js') { username: '#(creds.credentials.user)', password: '#(creds.credentials.pwd)' }

在空手道中,我建议尽可能坚持使用 JSON,除非您被迫使用 XML,因为您正在重新使用项目外部的东西或测试 SOAP 或 XML 有效载荷。但是如果你真的在做很多 XML 处理,请参考 this set of examples 的想法。

已编辑 - 因为我在表达式中遗漏了额外的 XML 根 credentials