1

I want to write data-driven tests passing dynamic values reading from external file (csv). Able to pass dynamic values from csv for simple strings (account number & affiliate id below). But, using embedded expressions, how can I pass dynamic values from csv file for "DealerReportFormats" json array below?

Any help is highly-appreciated!!

Scenario Outline: Dealer dynamic requests
    Given path '/dealer-reports/retrieval'
    And request read('../DealerTemplate.json')   
  When method POST
    Then status 200
    Examples: 
      | read('../DealerData.csv') | 


DealerTemplate.json is below
{    
  "DealerId": "FIXED",
  "DealerName": "FIXED",
  "DealerType": "FIXED",

  "DealerCredentials": {
    "accountNumber": "#(DealerCredentials_AccountNumber)",
    "affiliateId": "#(DealerCredentials_AffiliateId)"
  },
"DealerReportFormats": [
    {
      "name": "SalesReport",
      "format": "xml"
    },
    {
      "name": "CustomerReport",
      "format": "txt"
    }
  ]
}

DealerData.csv: 

DealerCredentials_AccountNumber,DealerCredentials_AffiliateId
testaccount1,123
testaccount2,12345
testaccount3,123456

jyohas
  • 41
  • 7
  • I tried understanding this question and gave up. anyone else here can take a shot. or you can follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Jun 18 '20 at 08:48
  • @PeterThomas I tried my best to simplify the question. Can you please suggest – jyohas Jun 18 '20 at 11:14
  • @jyohas Don't use csv as input. Instead, use json as input. That way you can have the json array as is in the input json. – Neodawn Jun 18 '20 at 11:15
  • @Neodawn csv is used to feed dynamic values into json template. wanted to pass dynamic values for 'name' and 'format' fields under DealerReportFormats (similar to how I did for #(DealerCredentials_AccountNumber)") – jyohas Jun 18 '20 at 11:21
  • @jyohas see my answer. you are trying to do too much in my honest opinion. sometimes, writing a few extra `Scenario`-s is fine: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Jun 18 '20 at 11:40
  • @PeterThomas Thanks for your answer. It worked. Since my example had an array I just modified it as : * json foo = [] – jyohas Jun 21 '20 at 03:15
  • @jyohas maybe the angle brackets are not needed. read this please: https://github.com/intuit/karate#scenario-outline-enhancements – Peter Thomas Jun 21 '20 at 04:07

1 Answers1

1

CSV is only for "flat" structures, so trying to mix that with JSON is too ambitious in my honest opinion. Please look for another framework if needed :)

That said I see 2 options:

a) use proper quoting and escaping in the CSV

b) refer to JSON files

Here is an example:

Scenario Outline:
* json foo = foo
* print foo

Examples:
| read('test.csv') |

And test.csv is:

foo,bar
"{ a: 'a1', b: 'b1' }",test1
"{ a: 'a2', b: 'b2' }",test2

I leave it as an exercise to you if you want to escape double-quotes. It is possible.

Option (b) is you can refer to stand-alone JSON files and read them:

foo,bar
j1.json,test1
j2.json,test2

And you can do * def foo = read(foo) in your feature.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248