How to use dynamic values for Karate Features

后端 未结 1 1825
感动是毒
感动是毒 2021-01-22 06:45

I\'ve a need where I should use dynamic values in the features of my karate tests.

I\'ve came accross with some of the questions and answers like this: How to read input

相关标签:
1条回答
  • 2021-01-22 07: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>
    """
    
    0 讨论(0)
提交回复
热议问题