如何从一个特征文件 table 中读取数据并将值传递到空手道 *.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?
功能文件1:inputData.功能
@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)'
文件 4:账户-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 以进行断言。
尝试
* def initTestData = call read('../inputData.feature')
* def reqRes = call read('../Request.feature') initTestData.testData
您可以使用 qaf web service support. In that case 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,request call的详细信息可以在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>
你的payload json文件可以如下(你也可以直接在正文中提供以下文件内容):
{
"channelData": {
"data": "CHANNEL_DATA",
"salesChannel": "WEB",
"createdBy": "WEBSITE",
"accountId": "${accountId}",
"sessionId": "${accountname}"
}
}
功能文件1:inputData.功能
@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)'
文件 4:账户-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 以进行断言。
尝试
* def initTestData = call read('../inputData.feature')
* def reqRes = call read('../Request.feature') initTestData.testData
您可以使用 qaf web service support. In that case 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,request call的详细信息可以在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>
你的payload json文件可以如下(你也可以直接在正文中提供以下文件内容):
{
"channelData": {
"data": "CHANNEL_DATA",
"salesChannel": "WEB",
"createdBy": "WEBSITE",
"accountId": "${accountId}",
"sessionId": "${accountname}"
}
}