2

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

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

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

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. 2.) also we to pass the expectedAccount value to Request.feature for assertion.

2 Answers 2

2

Try

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

refer data-driven in karate docs

Sign up to request clarification or add additional context in comments.

9 Comments

I am able to read the values from the data table. But failed in step * def resAccount = temp[0] with error "java.lang.RuntimeException: javascript evaluation failed: temp[0], Index: 0, Size: 0".
@Murugesh your temp might not have values populated properly. and hence when you try to access its 0th index you get index bound error. try * print reqRes as well as * print temp to see what data it is actually having. then only you can debug this
* print reqRes -- > [com.intuit.karate.ScriptObjectMap@48d6eea1] and for * print temp --> []. I got the expected value when I use,* temp = get[0] reqRes..account . What is the difference of using get[0] reqRes..account and $reqRes[*].account
I am not sure whether this will work but try * print karate.pretty(reqRes) to see the values on ScriptObjectMap object in JSON format
As the actual question has been already answered above, i request you to mark this answer as accepted and create a new question for your current issue.
|
0

You can make it simpler by using qaf web service support. In that case BDD file may look like below:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.