1
 * 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?

Sud
  • 166
  • 12

1 Answers1

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Works perfectly fine. Only one change is as below def data = { entity: ', phone: }. Since updategetPhonePref is already an embedded expression. Thanks for giving us the magic using Karate DSL [Peter](https://stackoverflow.com/users/143475/peter-thomas) – Sud Mar 16 '19 at 09:26