0

Below is my sample one JSON file, named as multiple.json and I have given the sample structure of my json file.

{
    "sample_key":[{"name":"John","salary":"10000","age":"25"}],
    "another_key":[{"author":"Scott","publication":"Pearson","year":"2019"}]
}

I have single feature file which has two Scenario Outline. This file is reading JSON object from multiple.json file which contains different JSON object separated by key value pair. And I want to pass the data in request payload which is sample_key and another_key in the below feature file.

My Sample.feature file:

Background:
    * def kittens = read('../json/multiple.json')

Scenario Outline: Create Sample Name Record
Given url url
And request { sample_key:'<sample_key>'}        // Here, I have question..
When method POST
Then status 200
* def output = response
* print output

Examples:
|kittens|

Scenario Outline: Create Sample Author Record
Given url url
And request { another_key:'<another_key>'}      // Here, I have question..
When method POST
Then status 200
* def output = response
* print output

Examples:
|kittens|

However, it is working only with below JSON with scenario.(Here, I do not have added JSON object in array)

{ "sample_key":{"name":"John","salary":"10000","age":"25"}, "another_key":{"author":"Scott","publication":"Pearson","year":"2019"} }

But it's not working when I give the JSON object in array (as mentioned at the top of question)

I want to pass multiple records in a single sample_key which I want to run it in a single scenario.

So, how can I pass sample_key in Scenario Outline: Create Sample Name Record?

Kindly suggest me. Thank you !!

Rucha
  • 139
  • 1
  • 8

1 Answers1

0

EDIT: maybe this is the answer you are looking for:

Examples:
| kittens.sample_key |

Maybe you don't need a Scenario Outline then. You can access JSON by index and just use 2 Scenario-s

* def sample_key = kittens.sample_key[0]

Also note that if you use a dynamic Scenario Outline you get an automatic __num variable: https://github.com/intuit/karate#scenario-outline-enhancements

So you can do this if kittens is a JSON array:

* def payload = kittens[__num]
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hi Peter, thank you for your response. I tried both the approach with `scenario` and with `scenario outline(__num)`. But none of them is working. However, it is working only with below `JSON` with `scenario`.(Here, I do not have added `JSON object in array`) `{ "sample_key":{"name":"John","salary":"10000","age":"25"}, "another_key":{"author":"Scott","publication":"Pearson","year":"2019"} }` – Rucha Apr 29 '20 at 10:00
  • @Rucha sorry, time for you to follow this process, or wait for someone else to help you: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Apr 29 '20 at 10:02
  • But, its not working when I give `JSON object in array` like below which is also mentioned in my question. `{ "sample_key":[{"name":"John","salary":"10000","age":"25"}], "another_key":[{"author":"Scott","publication":"Pearson","year":"2019"}] }` – Rucha Apr 29 '20 at 10:02
  • With reference to above `JSON`, I also have similar `JSON` with multiple records, mentioned as below: `{ "sample_key":[{"name":"John","salary":"10000","age":"25"},{"name":"Maria","salary":"20000","age":"30"}], "another_key":[{"author":"Scott","publication":"Pearson","year":"2019"},{"author":"Jammy","publication":"Oxford","year":"2020"}] }` So, basically, I want to pass multiple records in a single `sample_key` which I want to run it in a single `scenario`. Kindly suggest me. Thank you !! – Rucha Apr 29 '20 at 10:02
  • @Rucha sorry I'm not reading all these comments. all your answers are in the docs, or follow the process in the link above. all the best. – Peter Thomas Apr 29 '20 at 10:03
  • Hi @peter, I am not able to format my comments. please consider the same and help me with the solution. – Rucha Apr 29 '20 at 10:04
  • @Rucha you can edit your question. I suggest you keep it DEAD simple and ask only one thing, if you want any hope of getting an answer, also read this: https://stackoverflow.com/help/how-to-ask – Peter Thomas Apr 29 '20 at 10:06
  • @Rucha doesn't change my answer. I don't understand why you insist on reading `mutiple.json`. just have 2 normal scenarios and hard-code the `request`. you are un-necessarily complicating your test. also read this, and this is my last comment: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Apr 29 '20 at 10:18
  • 1
    Hi Peter, I understand what you are trying to explain me. The reason I wanted to include `request payload` into `JSON file` so that in future I can make the necessary changes in a `JSON` file. – Rucha Apr 29 '20 at 11:16