Search code examples
cucumber-jvmkarate

Calling karate feature with variable


In karate you can call feature by sending json/list & map. There are multiple ways I have tried to send the value and use it in the called feature but it throws error

For example:

Error thrown : path: $, actual: '', expected: '30e093da-c4e3-4ee0-b180-e5d0b4302d9f', reason: not a sub-string

Step:* call read('Logcheck.feature') {requestId : "#(responseHeaders['request-id'][0])"}

In the logCheck feature I am trying to get requestId by using #(requestId)

Logcheck feature Step

* def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')

I have tried another approach where I have assigned it to

* def x = {requestId : responseHeaders['request-id'][0]}
* call ('logcheck.feature;) x

Another approach where I will send the json

* json req = {requestId : "#(responseHeaders['request-id'][0])"} * call read('logcheck.feature') req

Step for logcheck.feature
* print req * def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')

For example:

Error thrown : com.intuit.karate.exception.KarateException: javascript evaluation failed: utils.getResultFromLog(baseUrl,'#> (requestId)','service'), null

print req output is

[print] {"requestId": "c996f752-c288-40c7-9398-c913254971e6"}


Solution

  • You need to be clear about using embedded expressions, please read the docs again: https://github.com/intuit/karate#rules-for-embedded-expressions

    For example this is wrong:

    * def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')
    

    Instead:

    * def resultFromServiceLog = utils.getResultFromLog(baseUrl, requestId, 'service')
    

    And this is wrong:

     * call ('logcheck.feature') x
    

    You probably meant:

    * call read('logcheck.feature') x