如何在空手道中迭代多个功能文件

时间:2019-01-18 06:46:29

标签: 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)'

文件4:addressinfo.feature

@ignore
Feature:

Scenario:
   * def Request = “{
     “accountId": "#(resAccount)”
    }
 ”
 * def headersData = { "Content-Type" : "application/json"}
 Given url BaseUrl +'#(resAccount)’+'/address’
 And request Request
 And headers headersData
 When method post
 Then status 200
 * print response

File5:Account-token.feature

  Feature: 
Scenario: identify the reference account
 * def initTestData = read('../inputData.feature')
 * def reqRes = karate.call('../Request.feature', { initTestData : initTestData })
 * def resAccount =  $reqRes[*].account // output of this is [“SB987658”,”SB984345”]
 * def addressData = read('../addressinfo.feature’,{resAccount: resAccount}) 

在上述情况下,我们必须将Request.feature的输出作为输入传递给addressing.feature。 java.net.URISyntaxException:索引34:http://10.36.145.196:9983/invoker/[“ SB987658”,“ SB984345”] / address处路径中的非法字符。我们的要求是应该迭代resAccount的每个值,并且需要将addressinfo.feature的o / p响应作为I / p传递到另一个功能文件。

1 个答案:

答案 0 :(得分:1)

call args需要循环的规则是它必须是JSON数组。请参阅:https://github.com/intuit/karate#data-driven-features

因此您可以变换基本数组,请参考:https://github.com/intuit/karate#json-transforms

* def resAccountList = karate.map(resAccount, function(x){ return { resAccount: x } })

我必须说您的测试设计不当,将来会给您带来维护问题。尝试避免“过多地重复使用”,并尽可能避免使用call。有关原因,请参考此答案:https://stackoverflow.com/a/54126724/143475