简体   繁体   English

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

[英]How to read data from one feature file table and pass the value to set json parameter in a *.json file in karate?

Feature File 1:inputData.feature特征文件 1:inputData.feature

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

File 2: payload.json文件 2:payload.json

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

File 3: Request.feature文件 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 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]

In the above scenario values are not passed successfully in JSON Request.: 1.) We need to read the accountId & accountname value from inputData.feature, and update the payload.json parameters.在上述场景中,JSON 请求中的值未成功传递。: 1.) 我们需要从 inputData.feature 中读取 accountId 和 accountname 值,并更新 payload.json 参数。 2.) also we to pass the expectedAccount value to Request.feature for assertion. 2.) 我们也将 expectedAccount 值传递给 Request.feature 进行断言。

Try尝试

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

refer data-driven in karate docs参考空手道文档中的数据驱动

You can make it simpler by using qaf web service support .您可以通过使用qaf Web 服务支持使其更简单。 In that case BDD file may look like below:在这种情况下, BDD 文件可能如下所示:

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'    |

Where my.sample.reqwithbody1 is request call and request call details can be in request call repository that can be reused and may read as below:其中my.sample.reqwithbody1是请求调用,请求调用详细信息可以在可以重用的请求调用存储库中,如下所示:

<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>

Your payload json file can be as below(You also can provide below file content directly in body):您的有效负载 json 文件可以如下(您也可以直接在正文中提供以下文件内容):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从JSON文件传递Cucumber功能中的参数值? - Is it possible to pass parameter value in Cucumber feature from JSON file? 如何将一些 json 文件中的数据传递到 Gherkin 功能文件 - How to pass a data from some json file to Gherkin feature file 如何在Cucumber中将原始数据json格式从特征文件传递到stepdefinition文件 - How to pass raw data json format from feature file to stepdefinition file in Cucumber 如何从 excel 电子表格中读取输入数据并在空手道框架中传递 JSON 有效载荷? - How to read input data from an excel spreadsheet and pass it JSON payload in karate framework? 如何在空手道dsl中读取和编辑json文件并将其作为请求发送 - How to read and edit a json file and send it as a request in karate dsl 如何从功能文件准备json文件? - How to prepare json file from feature file? 如何将参数传递给Selenium Java测试文件中的空手道特征文件 - How to pass arguments to karate feature file from selenium java test file 如何将一个特征文件的数据(从示例中)传递到另一个特征文件? - how to pass the data(from Examples) of one feature file to other feature file? 如何在空手道框架的特征文件中正确传递表单字段范围? - How to pass the form field scope correctly in feature file of karate framework? 如何在空手道框架的特征文件中读取响应头值? - how to read response header values in feature file of karate framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM