Karate : Dynamically input values from embedded expressions in a JSON file

三世轮回 提交于 2019-12-13 00:29:35

问题


 * def mpRequestJson =
        """
        {
            "entity": '<entity>',
            "consent": {
                "PHONE": <updategetPhonePref>,
                "EMAIL": true,
                "POST": false,
                "SMS": <updategetSMSPref>
            },
            "notices": [{
                "title": "Privacy policy",
                "version": "NA"
            }],
            "source": "web"
        }
        """
        Given path '<entity>'
        And request mpRequestJson
        When method PUT
        Then status 200
        And match $.consent.PHONE == '<updategetPhonePref>'
        And match $.consent.SMS == '<updategetSMSPref>'

        Examples:
            |entity  | updategetPhonePref|updategetSMSPref|
            |xyz| #(updategetPhonePref)|#(updategetSMSPref)|

If i want to store the JSON request in a JSON file rather than in the feature file, what should be my JSON file?


回答1:


In the JSON use embedded expressions, e.g.

entity: '#(entity)'

Then you can read it from a file:

* def mpRequestJson = read('my.json')

But before the read you should initialize variables that have to be substituted. So you will have some extra lines.

* def entity = '<entity>'

One way to reduce the extra lines is to create a temp JSON:

* def data = { entity: '<entity'>, phone: '<updategetPhonePref>' }

And then you can do this in the JSON:

entity: '#(data.entity)'

Read the docs on data driven tests also please.



来源:https://stackoverflow.com/questions/55192349/karate-dynamically-input-values-from-embedded-expressions-in-a-json-file

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