cheparsky
cheparsky

Reputation: 532

Karate: When sending jsonPath to another feature, 'set' function don't see that jsonPath

I have two feature: first where I have arguments and second where I want to send argumnets from first feature.

First feature:

Feature: test
@dev
  Scenario: test

    * def arguments = { value: '123.00', jspath: '..transferData.amount',consent_body: 'classpath:consent_domestic.json' }
    * def createConsentResult = call read('classpath:reuseable/features/changeConsentBody.feature') arguments

Second feature:

@ignore
Feature: Change consent body - 1 parameter

  Background:
    * url ''
    * call read('classpath:reuseable/features/commonFunction.feature')

  @act
  Scenario : Change consent body - 1 parameter

    * path 'consents'
    * def consentBody = read(consent_body)
    * print "jsonPath: "+jspath
    * set consentBody jspath= value
    When request consentBody
    And method post
    Then status 201

Here * def consentBody = read(consent_body) karate sees 'consent_body' as a variable and uses value of this variable.

Here * print "jsonPath: "+jspath karate sees 'jspath' as a variable and uses value of this variable and print: [print] jsonPath: ..transferData.amount.

BUT here * set consentBody jspath = value karate doesn't see 'jspath' as a variable and doesn't use the value of this variable. Instead , karate shows error:

changeConsentBody.feature:17 - unexpected path: jspath

In this case karate need to see 'jspath' as a variable and use the value of this variable.

Upvotes: 1

Views: 1987

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

I see two obvious problems here. By the way, please maintain white-space on each side of the = sign:

* set consentBody jspath = value

Variables will not work for the set command. And you cannot use JsonPath. If you have dynamic paths, you can do this, refer: https://github.com/intuit/karate#eval

* def foo = {}
* def path = 'bar'
* eval foo[path] = 'baz'
* match foo == { bar: 'baz' }

If you want to update an array in "bulk", instead of ..transferData.amount - please use the karate.map() operation:

* def foo = [{},{}]
* def fun = function(x){ x.transferData = { amount: 100 }; return x }
* def res = karate.map(foo, fun)
* match res == [{ transferData: { amount: 100 } }, { transferData: { amount: 100 } }]

Finally I urge you to read this carefully, try to avoid "generic" test cases and using call, it just complicates things un-necessarily: https://stackoverflow.com/a/54126724/143475

Upvotes: 1

Related Questions