2

Requirement: I want to iterate each row in example section on the basis of a field (ProductType) present in input Json.

ProductType can have value 11 or 22 or 33. But expectedAmount for those should be same. I am giving a snippet below-

**Feature:** To check expected results for different values of ProductType

  Background: 
    * url http://localhost:8080
    * header Content-Type = 'application/json'

  Scenario Outline: To check expected results for different values of ProductType
    Given path PATH
    And request <data>
    When method POST
    Then status 200
    And match $.Parent[0].Amount ==  expectedAmount>
Examples: 
  | data                                                 | ExpectedAmount |
  | read('classpath:' + INPUT_JSON_PATH + '/Test1.json') |        1234.56 |
  | read('classpath:' + INPUT_JSON_PATH + '/Test2.json') |        6789.12 |
  | read('classpath:' + INPUT_JSON_PATH + '/Test3.json') |        3453.56 |

Json Input:

{
      "Parent" : [ {
      "Id" : "1",
      "productType" : 11
      }]

}

Can you please suggest a solution for it?

Thanks, Abhi

Neodawn
  • 1,086
  • 1
  • 6
  • 9
Abhi
  • 309
  • 1
  • 10

1 Answers1

0

The Examples: rows do not support any variable evaluation e.g. read('classpath:' + INPUT_JSON_PATH + '/Test1.json') so please do this instead:

* def data = read('classpath:' + INPUT_JSON_PATH + '/' + file)

Examples: 
  | file       | ExpectedAmount |
  | Test1.json |        1234.56 |
  | Test2.json |        6789.12 |
  | Test3.json |        3453.56 |

I'm not reading the rest of your question because I still don't understand it.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hi Peter. example row evaluates variable perfectly for me and works fine. But Anyways lets take up the example section which you have mentioned. Here if I want to run Test1.Json 3 times based on different values of productType ( a field in input json) then how can that be done? – Abhi May 15 '20 at 14:44
  • @AbhitanshuShukla it shouldn't – Peter Thomas May 15 '20 at 14:45
  • Ohh that's a shame then. Usually Karate has lot of rich features, and Iteration on a Json variable looks like a must have feature for API testing. – Abhi May 15 '20 at 14:53
  • @AbhitanshuShukla why don't you take a week off and just read the entire documentation. then come back and share what you have learned: https://github.com/intuit/karate#table – Peter Thomas May 15 '20 at 15:08
  • @AbhitanshuShukla you can have the product types in the Example section. So for 10 products you need 10 lines (if it is dynamic, you can read from csv too). you can then have the three requests for the validations one after another within a single scenario. – Neodawn May 15 '20 at 18:41
  • Thanks @Neodawn for your reply . I already have tried that way. In my input Json i kept "productType" : '#(ProductType)' and gave ProductType in example section. But if it will create extra rows in example section. For example if Test1, Test2 and Test3.json each need to be iterated over 3 product types then I will have to keep 9 rows in example section. My Product types are static and not dynamic. Do you suggest to have another Json to keep just product types or can we have a simple java script which can iterate each line of example section based on different values of product types? – Abhi May 16 '20 at 07:40
  • thanks @Neodawn for taking care of this ;) here's one more reference: https://stackoverflow.com/a/61685169/143475 – Peter Thomas May 16 '20 at 07:54
  • 1
    @AbhitanshuShukla I think you are still thinking in terms one request per Scenario/Scenario Outline. A Scenario/Scenario can have multiple requests and validations (Given+When+Then combinations). Sample code: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/examples.feature – Neodawn May 16 '20 at 09:01
  • 1
    @AbhitanshuShukla You can validate Test1, Test2 & Test3 one after another within the same Scenario Outline. In such a setup you only need 3 rows in Examples for 3 product types. Since you stated that the product types is a static count, you can keep the values in a JSON array (either directly defined in the feature file or read from a json file) and just enter the variable in the Example section as in the following example: https://stackoverflow.com/a/54398100/442595 – Neodawn May 16 '20 at 09:02
  • Thanks @Neodawn, I get your point now and I am able to make such set up. Now I have 3 combinations of Given When Then under Scenario Outline and Just one column of ProductTypes under examples which contains 3 rows. But in my report, I see there are 3 scenario run which is obvious because I have 3 ProductTypes. Can I have a way where my report will also show that 9 scenarios have run ? – Abhi May 17 '20 at 16:05
  • 1
    @AbhitanshuShukla The simplest way to achieve that is to split into Test1, Test2 and Test3 validations into three separate scenario outlines. – Neodawn May 17 '20 at 16:20
  • @Neodawn - In that case I'll have to keep ProductType in each example section which I wanted to avoid. In real scenario I have about 20 Test Json files which needs to be iterated for 10 ProductType. For easy understanding I gave 3 test json and 3 Product type in my original question. – Abhi May 17 '20 at 17:30
  • 1
    @AbhitanshuShukla keep the product types in a file as shown in this answer: https://stackoverflow.com/a/54398100/442595. That way you will have only one line in the example section. Later if you want to add a product type, just add it in the file and it will reflect in all scenarios. – Neodawn May 17 '20 at 17:40
  • Many Thanks @Neodawn. Finally I am able to achieve the objective. Really appreciate it. – Abhi May 18 '20 at 07:55