如何从一个功能文件表中读取数据并在空手道的* .json文件中传递值以设置json参数?

时间:2019-01-08 17:36:55

标签: cucumber karate

功能文件1:inputData.feature

@ignore
 Feature: Input data table
  Scenario: Input table for testing
    * table testData
      | accountId           |  accountname    | expectedAccount  |
      | 'ADS5678543'   | 'Peter'                 | 'DFCVSAEFRG'      |
      | 'ASDCF45678'   | 'Caroline'            |  'DFCWEAEFRG'    |

文件2:payload.json

{
  "channelData": {
    "data": "CHANNEL_DATA",
    "salesChannel": "WEB",
    "createdBy": "WEBSITE",
    "accountId": "#(accountId)",
    "sessionId": "#(accountname)"
     }
}

文件3:Request.feature

@ignore
Feature:

Scenario:
  # read the payload from json file
  * def Request = read('../payload.json')
  * def headersData = { "Content-Type" : "application/json"}
  Given url BaseUrl + '/account/'+'#(accountId)'
  And request Request
  And headers headersData
  When method post
  Then status 200
  * print response
  * def account = karate.jsonPath(response, "$.account")
  * print 'account is '+account
  Then match account == '#(expectedAccount)'

File4:Account-token.feature

Feature: 
Scenario: identify the reference account
     * def initTestData = read('../inputData.feature')
     * def reqRes = karate.call('../Request.feature', { initTestData : initTestData })
     * def temp =  $reqRes[*].account
     * def resAccount = temp[0]

在上述情况下,未在JSON请求中成功传递值:    1.)我们需要从inputData.feature中读取accountId和accountname值,并更新payload.json参数。    2.)另外,我们还将ExpectedAccount值传递给Request.feature进行断言。

2 个答案:

答案 0 :(得分:2)

尝试

* def initTestData = call read('../inputData.feature')
* def reqRes = call read('../Request.feature') initTestData.testData

推荐data-driven in karate docs

答案 1 :(得分:0)

您可以使用qaf web service support使其更简单。在这种情况下,BDD file可能如下所示:

Feature: Input data table
  Scenario: Input table for testing
    Given  user requests "my.sample.reqwithbody1" with data "${args[0]}"
    Then response should have status code 200
    And response should have "${expectedAccount}" at "$.account"
    And say "resAccount" is value at jsonpath "$.account"
  Examples:
      | accountId      |  accountname    | expectedAccount  |
      | 'ADS5678543'   | 'Peter'         | 'DFCVSAEFRG'     |
      | 'ASDCF45678'   | 'Caroline'      |  'DFCWEAEFRG'    |

my.sample.reqwithbody1是请求呼叫,请求呼叫详细信息可以在request call repository中,可以重复使用,其内容如下:

<my>
  <sample>
    <reqwithbody1>
       <endPoint>/account/${accountId}</endPoint>
       <headers>
          {'Content-Type': 'application/json'}
      </headers>
      <method>POST</method>
      <body>file:resources/data/payload.json</body>
    </reqwithbody1>

  </sample>
</my>

您的有效负载json文件可以如下所示(您也可以直接在主体中提供以下文件内容):

{
  "channelData": {
    "data": "CHANNEL_DATA",
    "salesChannel": "WEB",
    "createdBy": "WEBSITE",
    "accountId": "${accountId}",
    "sessionId": "${accountname}"
     }
}