unable to update variables in a called feature

早过忘川 提交于 2021-02-17 03:38:19

问题


I am trying to follow the examples in the demo: https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/callfeature I need to do a call from one feature to another, and pass a reference to update. The reference is for a JSON that is read from a file:

  Background: 
    * url url
    * header Authorization = token
    * def payload = read('event.json')
    * set payload.createdByUser = 'karate'

  Scenario: Call another feature with arg
    * call read('classpath:common/swap-json-elements.feature') payload
    * print payload

Inside my swap-json-elements.feature:

  Background: 
    * set new = payload.old
    * set payload.new= payload.old
    * set payload.old= new

This is not working. It is clear in the documentation that a shared scope is shared when we 'set' is used, while 'def' will create a new variable, and never update the shared one.

What am I missing ?


回答1:


If you pass an argument, it is passed by value. When you call with "shared scope" you typically don't need to pass arguments. Because all variables are visible anyway. Try a simpler example, and please watch white-space around the = sign.

main.feature:

Feature:

Background:
* def json = { foo: 'bar' }
* call read('called.feature')

Scenario:
* match json == { foo: 'baz' }

called.feature

Feature:

Scenario:
* set json.foo = 'baz'
* match json == { foo: 'baz' }


来源:https://stackoverflow.com/questions/54031473/unable-to-update-variables-in-a-called-feature

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!