1

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.

cheparsky
  • 514
  • 6
  • 20

1 Answers1

1

Sorry your question has to be simplified if you really want people to have the patience to go through and understand the problem. Please refer to this in future: https://stackoverflow.com/help/mcve

That said, 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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    one more way would be to use the embedded expression in your `consent_domestic.json`, you can add `'#(value)'` as a value for all the `amount` you want to modify. something like this `{ transferData: { amount: '#(value)'} }`. – Babu Sekaran Feb 20 '19 at 13:44
  • If _'Variables will not work for the set command.'_, then why for 'consentBody' and 'value' it is working? For two of three params it is working and I think it will be logical if it will be working for the third too. – cheparsky Feb 20 '19 at 14:22
  • @cheparsky `set` is useful for 90% of the folks who write simple test cases without trying to be too "generic". if you feel that it is not right, assume that the `set` keyword does not exist and stick to using `eval`. – Peter Thomas Feb 20 '19 at 15:06