How to use dynamic values for Karate Features

 ̄綄美尐妖づ 提交于 2019-12-02 07:53:13

I am providing a simplified example below, but am sure you will find the answers to your questions here. It is easy to loop over data and build XML in Karate using the karate.set(varName, xPath, value) API:

* table users
  | accountNo   | subsID         | mobile       | subsType  |
  | '113888572' | '113985218890' | '1135288836' | 'asd'     |
  | '113888573' | '113985218891' | '1135288837' | 'qwe'     |
  | '113888582' | '113985218810' | '1135288846' | 'asd'     |

* def xml = <users></users>
* def fun =
"""
function(u, i) {
  var base = '/users/user[' + (i + 1) + ']/';
  karate.set('xml', base + 'account', u.accountNo);
  karate.set('xml', base + 'mobile', u.mobile);
  karate.set('xml', base + 'type', u.subsType);
}
"""
* eval karate.forEach(users, fun)
* match xml ==
"""
<users>
  <user>
    <account>113888572</account>
    <mobile>1135288836</mobile>
    <type>asd</type>
  </user>
  <user>
    <account>113888573</account>
    <mobile>1135288837</mobile>
    <type>qwe</type>
  </user>
  <user>
    <account>113888582</account>
    <mobile>1135288846</mobile>
    <type>asd</type>
  </user>
</users>
"""
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!